代码拉取完成,页面将自动刷新
import socket
import _thread
from request import Request
from utils import log
from routes import error
from routes.routes_todo import route_dict as todo_routes
from routes.api_todo import route_dict as todo_api
from routes.api_weibo import route_dict as weibo_api
from routes.routes_weibo import route_dict as weibo_routes
from routes.routes_user import route_dict as user_routes
from routes.routes_public import route_dict as public_routes
def response_for_path(request):
"""
根据 path 调用相应的处理函数
没有处理的 path 会返回 404
"""
r = {}
# 注册外部的路由
r.update(todo_api())
r.update(weibo_api())
r.update(todo_routes())
r.update(weibo_routes())
r.update(user_routes())
r.update(public_routes())
response = r.get(request.path, error)
log('request', request, response)
return response(request)
def process_request(connection):
with connection:
r = connection.recv(1024)
log('request log:\n <{}>'.format(r))
r = r.decode()
# 把原始请求数据传给 Request 对象
request = Request(r)
# 用 response_for_path 函数来得到 path 对应的响应内容
response = response_for_path(request)
log("response log:\n <{}>".format(response))
# 把响应发送给客户端
connection.sendall(response)
def run(host, port):
"""
启动服务器
"""
# 使用 with 可以保证程序中断的时候正确关闭 socket 释放占用的端口
log('开始运行于', 'http://{}:{}'.format(host, port))
with socket.socket() as s:
s.bind((host, port))
# 监听 接受 读取请求数据 解码成字符串
s.listen()
# 无限循环来处理请求
while True:
connection, address = s.accept()
# 第二个参数类型必须是 tuple
log('ip {}'.format(address))
_thread.start_new_thread(process_request, (connection,))
if __name__ == '__main__':
# 生成配置并且运行程序
config = dict(
host='127.0.0.1',
port=2000,
)
run(**config)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。