- 网络 udp
- 多任务 线程
- 多任务 线程、进程
- 网络 tcp
- 飞鸽传书 完善
- 多任务 协程
- 正则表达式
- 网络通信过程、http 协议
- Web 服务器 并发服务器
- WSGI、mini Web 框架
- 装饰器、mini Web 框架 路由
- MySQL 基本使用
- MySQL 查询
- MySQL 与 Python 交互
- mini Web 框架 添加 MySQL 功能
- 其它知识
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Web 静态服务器 6 非堵塞模式
Web静态服务器-6-非堵塞模式
单进程非堵塞 模型
#coding=utf-8
from socket import *
import time
# 用来存储所有的新链接的socket
g_socket_list = list()
def main():
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR , 1)
server_socket.bind(('', 7890))
server_socket.listen(128)
# 将套接字设置为非堵塞
# 设置为非堵塞后,如果accept时,恰巧没有客户端connect,那么accept会
# 产生一个异常,所以需要try来进行处理
server_socket.setblocking(False)
while True:
# 用来测试
time.sleep(0.5)
try:
newClientInfo = server_socket.accept()
except Exception as result:
pass
else:
print("一个新的客户端到来:%s" % str(newClientInfo))
newClientInfo[0].setblocking(False) # 设置为非堵塞
g_socket_list.append(newClientInfo)
for client_socket, client_addr in g_socket_list:
try:
recvData = client_socket.recv(1024)
if recvData:
print('recv[%s]:%s' % (str(client_addr), recvData))
else:
print('[%s]客户端已经关闭' % str(client_addr))
client_socket.close()
g_socket_list.remove((client_socket,client_addr))
except Exception as result:
pass
print(g_socket_list) # for test
if __name__ == '__main__':
main()
web静态服务器-单进程非堵塞
import time
import socket
import sys
import re
class WSGIServer(object):
"""定义一个WSGI服务器的类"""
def __init__(self, port, documents_root):
# 1. 创建套接字
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定本地信息
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind(("", port))
# 3. 变为监听套接字
self.server_socket.listen(128)
self.server_socket.setblocking(False)
self.client_socket_list = list()
self.documents_root = documents_root
def run_forever(self):
"""运行服务器"""
# 等待对方链接
while True:
# time.sleep(0.5) # for test
try:
new_socket, new_addr = self.server_socket.accept()
except Exception as ret:
print("-----1----", ret) # for test
else:
new_socket.setblocking(False)
self.client_socket_list.append(new_socket)
for client_socket in self.client_socket_list:
try:
request = client_socket.recv(1024).decode('utf-8')
except Exception as ret:
print("------2----", ret) # for test
else:
if request:
self.deal_with_request(request, client_socket)
else:
client_socket.close()
self.client_socket_list.remove(client_socket)
print(self.client_socket_list)
def deal_with_request(self, request, client_socket):
"""为这个浏览器服务器"""
if not request:
return
request_lines = request.splitlines()
for i, line in enumerate(request_lines):
print(i, line)
# 提取请求的文件(index.html)
# GET /a/b/c/d/e/index.html HTTP/1.1
ret = re.match(r"([^/]*)([^ ]+)", request_lines[0])
if ret:
print("正则提取数据:", ret.group(1))
print("正则提取数据:", ret.group(2))
file_name = ret.group(2)
if file_name == "/":
file_name = "/index.html"
# 读取文件数据
try:
f = open(self.documents_root+file_name, "rb")
except:
response_body = "file not found, 请输入正确的url"
response_header = "HTTP/1.1 404 not found\r\n"
response_header += "Content-Type: text/html; charset=utf-8\r\n"
response_header += "Content-Length: %d\r\n" % (len(response_body))
response_header += "\r\n"
# 将header返回给浏览器
client_socket.send(response_header.encode('utf-8'))
# 将body返回给浏览器
client_socket.send(response_body.encode("utf-8"))
else:
content = f.read()
f.close()
response_body = content
response_header = "HTTP/1.1 200 OK\r\n"
response_header += "Content-Length: %d\r\n" % (len(response_body))
response_header += "\r\n"
# 将header返回给浏览器
client_socket.send( response_header.encode('utf-8') + response_body)
def main():
"""控制web服务器整体"""
# python3 xxxx.py 7890
if len(sys.argv) == 2:
port = sys.argv[1]
if port.isdigit():
port = int(port)
else:
print("运行方式如: python3 xxx.py 7890")
return
print("http服务器使用的port:%s" % port)
http_server = WSGIServer(port, "./html")
http_server.run_forever()
if __name__ == "__main__":
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论