2 Star 4 Fork 4

spacexzm/socketMVC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.py 2.10 KB
一键复制 编辑 原始数据 按行查看 历史
spacexzm 提交于 2019-08-06 11:23 . delete some useless annotation
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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/spacexzm/socketMVC.git
git@gitee.com:spacexzm/socketMVC.git
spacexzm
socketMVC
socketMVC
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385