我可以使用本教程提供 http2。https://python-hyper.org/projects/h2/en/stable/basic-usage.html。这个版本运行良好。但是由于阻塞套接字,该服务器一次只能为一个客户端提供服务。然后我寻找基于系统调用的多客户端服务器实现select()
。本教程向我展示了如何做到这一点。https://realpython.com/python-sockets/。我合并了两个教程,以创建一个 http2 多客户端服务服务器。这就是我最终的结果:
服务器.py
import json
import socket
import h2.connection
import h2.events
import types
from thread import start_new_thread
try:
import selectors
except ImportError:
import selectors2 as selectors
sel = selectors.DefaultSelector()
h2conn = h2.connection.H2Connection(client_side=False)
def send_response(hyperConn, event):
print ('sending respond')
stream_id = event.stream_id
response_data = json.dumps(dict(event.headers))+'KUKA MAKI'.encode('utf-8')
hyperConn.send_headers(
stream_id=stream_id,
headers=[
(':status', '200'),
('server', 'basic-h2-server/1.0'),
('content-length', str(len(response_data))),
('content-type', 'application/json'),
],
)
hyperConn.send_data(
stream_id=stream_id,
data=response_data,
end_stream=True
)
def accept_wrapper(sock):
conn, addr = sock.accept() # Should be ready to read
print("accepted connection from", addr)
conn.setblocking(False)
data = lambda: None # Dummy namespace
data.addr = addr
data.inb = b''
data.outb = b''
events = selectors.EVENT_READ | selectors.EVENT_WRITE
sel.register(conn, events, data=data)
h2InitConn = h2.connection.H2Connection(client_side=False)
h2InitConn.initiate_connection()
conn.sendall(h2InitConn.data_to_send())
def service_connection(key, mask):
sock = key.fileobj
data = key.data
if mask & selectors.EVENT_READ:
recv_data = sock.recv(65535) # Should be ready to read
if recv_data:
# data.outb += recv_data
print ('raw data: ', recv_data)
events = h2conn.receive_data(recv_data)
print 'events received:' , events
for event in events:
if isinstance(event, h2.events.RequestReceived):
print ('request recieved')
send_response(h2conn, event)
else:
print("closing connection to", data.addr)
sel.unregister(sock)
sock.close()
if mask & selectors.EVENT_WRITE:
data_to_send = h2conn.data_to_send()
if data_to_send:
print("replying with stuff to", data.addr)
sock.sendall(data_to_send)
lsock = socket.socket()
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host, port = '0.0.0.0' , 8080
lsock.bind((host, port))
lsock.listen(5)
print("listening on", (host, port))
lsock.setblocking(False)
sel.register(lsock, selectors.EVENT_READ, data=None)
try:
while True:
events = sel.select(timeout=None)
for key, mask in events:
if key.data is None:
accept_wrapper(key.fileobj)
else:
service_connection(key, mask)
except KeyboardInterrupt:
print("caught keyboard interrupt, exiting")
finally:
sel.close()
如果我运行 python server.py,并在另一个终端中运行超级 CLI 工具,以发送 http2 请求 ( hyper --h2 GET http://localhost:8080/
)。第一个请求成功,我可以看到生成的事件,超级工具显示响应并很好地退出。之后再次发送相同的超级命令,请求没有生成任何事件,CLI 工具也挂起。你能帮我解决问题吗?