def upgrade_to_websocket(http_protocol):
request_headers = dict(http_protocol.headers)
response_headers = []
def get_header(key):
key = key.lower().encode('utf-8')
return request_headers.get(key, b'').decode('utf-8')
def set_header(key, value):
response_headers.append((key.encode('utf-8'), value.encode('utf-8')))
try:
key = websockets.handshake.check_request(get_header)
websockets.handshake.build_response(set_header, key)
except websockets.InvalidHandshake:
http_protocol.send({
'status': 400,
'headers': [[b'Content-Type', b'text/plain']],
'content': b'Invalid Handshake'})
websocket = WebSocketProtocol(http_protocol, response_headers)
websocket.connection_made(http_protocol.transport,
http_protocol.parser.get_http_version(),
http_protocol.parser.get_method(),
http_protocol.url,
http_protocol.headers)
http_protocol.transport.set_protocol(websocket)
评论列表
文章目录