python web自制框架之-完整服务端实现
2018-02-13 16:01
211 查看
今天我们做下服务端的完整实现,把路由与模型分开来。
首先是run函数,实现接收请求与返回客户端数据
1.def run(host='', port=3000):
"""
启动服务器
"""
# 初始化 socket 套路
log('start at', '{}:{}'.format(host, port))
with socket.socket() as s:
s.bind((host, port))
# 无限循环来处理请求
while True:
# 监听 接受 读取请求数据 解码成字符串
s.listen(5)
connection, address = s.accept()
r = connection.recv(1000)
r = r.decode('utf-8')
# log('ip and request, {}\n{}'.format(address, request))
# 因为 chrome 会发送空请求导致 split 得到空 list
# 所以这里判断一下防止程序崩溃
if len(r.split()) < 2:
continue
path = r.split()[1]
# 设置 request 的 method
request.method = r.split()[0]
# 把 body 放入 request 中
request.body = r.split('\r\n\r\n', 1)[1]
# 用 response_for_path 函数来得到 path 对应的响应内容
response = response_for_path(path)
# 把响应发送给客户端
connection.sendall(response)
# 处理完请求, 关闭连接
connection.close()这个是完整的服务器接收与返回数据(客户端).
这里的注意:
with socket.socket() as s:
response = response_for_path(path)response_for_path(path)为函数,如下:def response_for_path(path):
path, query = parsed_path(path)
request.path = path
request.query = query
log('path and query', path, query)
"""
根据 path 调用相应的处理函数
没有处理的 path 会返回 404
"""
r = {
'/static': route_static,
# '/': route_index,
# '/login': route_login,
# '/messages': route_message,
}
r.update(route_dict)
response = r.get(path, error)
log('request',response(request))
return response(request)这里的注意:
path, query = parsed_path(path)parsed_path是得路径与参数,具体代码就不贴了,如果需要,可以留言。
path里是路径,query里存的参数。
r = {
'/static': route_static,
# '/': route_index,
# '/login': route_login,
# '/messages': route_message,
}这里,我们刚开始是在这里存路由,现在我们把路由放在别的文件中,所以,这里只是放着静态文件的路径。
r.update(route_dict)r更新,把route_dict里的路径添加进去。如下route_dict = {
'/': route_index,
'/login': route_login,
'/register': route_register,
'/messages': route_message,
}到这里,我们就完成了简单的mvc模式分离的一部分。
首先是run函数,实现接收请求与返回客户端数据
1.def run(host='', port=3000):
"""
启动服务器
"""
# 初始化 socket 套路
log('start at', '{}:{}'.format(host, port))
with socket.socket() as s:
s.bind((host, port))
# 无限循环来处理请求
while True:
# 监听 接受 读取请求数据 解码成字符串
s.listen(5)
connection, address = s.accept()
r = connection.recv(1000)
r = r.decode('utf-8')
# log('ip and request, {}\n{}'.format(address, request))
# 因为 chrome 会发送空请求导致 split 得到空 list
# 所以这里判断一下防止程序崩溃
if len(r.split()) < 2:
continue
path = r.split()[1]
# 设置 request 的 method
request.method = r.split()[0]
# 把 body 放入 request 中
request.body = r.split('\r\n\r\n', 1)[1]
# 用 response_for_path 函数来得到 path 对应的响应内容
response = response_for_path(path)
# 把响应发送给客户端
connection.sendall(response)
# 处理完请求, 关闭连接
connection.close()这个是完整的服务器接收与返回数据(客户端).
这里的注意:
with socket.socket() as s:
# 使用 with 可以保证程序中断的时候正确关闭 socket 释放占用的端口request是个全局变量,为了接收数据而设,在我上篇文章里有介绍。
response = response_for_path(path)response_for_path(path)为函数,如下:def response_for_path(path):
path, query = parsed_path(path)
request.path = path
request.query = query
log('path and query', path, query)
"""
根据 path 调用相应的处理函数
没有处理的 path 会返回 404
"""
r = {
'/static': route_static,
# '/': route_index,
# '/login': route_login,
# '/messages': route_message,
}
r.update(route_dict)
response = r.get(path, error)
log('request',response(request))
return response(request)这里的注意:
path, query = parsed_path(path)parsed_path是得路径与参数,具体代码就不贴了,如果需要,可以留言。
path里是路径,query里存的参数。
r = {
'/static': route_static,
# '/': route_index,
# '/login': route_login,
# '/messages': route_message,
}这里,我们刚开始是在这里存路由,现在我们把路由放在别的文件中,所以,这里只是放着静态文件的路径。
r.update(route_dict)r更新,把route_dict里的路径添加进去。如下route_dict = {
'/': route_index,
'/login': route_login,
'/register': route_register,
'/messages': route_message,
}到这里,我们就完成了简单的mvc模式分离的一部分。
相关文章推荐
- 使用Python的Flask框架表单插件Flask-WTF实现Web登录验证
- 使用Python的web.py框架实现类似Django的ORM查询的教程
- 使用Python的Tornado框架实现一个Web端图书展示页面
- python web自制框架之接受url传递过来的参数
- werkzeug实现简单Python web框架(2):添加jinjia2模板支持
- werkzeug实现简单Python web框架(3):添加动态路由
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- 使用Python的web.py框架实现类似Django的ORM查询的教程
- [python]web框架中的代码自动重载怎么实现
- python 实现web框架simfish
- Python实现手写一个类似django的web框架示例
- web框架的原理以及web框架的实现(python)
- [python]web框架中的代码自动重载怎么实现
- 使用Python的Tornado框架实现一个Web端图书展示页面
- werkzeug实现简单Python web框架(4):添加orm支持
- Python基于WSGI实现mini_web框架及服务器
- 完整使用SSM框架(Spring SpringMVC Mybatis)搭建web服务器实现登录功能
- werkzeug实现简单Python web框架(1):MVC模式
- werkzeug实现简单Python web框架(5):扩展思路
- 装饰器实现Python web框架路由功能