websocket 与 tornado 的结合

时间:2021-10-17 05:17:33

对于socket是不陌生的,但是对于websocket我却是陌生的,不同于https,在网页中使用websocket可以同样起到ajax的作用,默默发送数据。。。

在script中:

        ws = new WebSocket("ws://127.0.0.1:8888/chat");
//ws接收信息
ws.onmessage = function (event) {
$('.container').append(event.data);
};
//服务端关闭,触发函数
ws.onclose = function () { };
//发送信息
function sendMessage() {
ws.send($('#txt').val());
}

在tornado中有专门针对websocket请求的模块,不再是https的tornado.web.Resquest而是tornado.websocket :

import tornado.web
import tornado.ioloop
import tornado.websocket
class IndexHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs):
self.render('index.html') users = set()
class ChatHandler(tornado.websocket.WebSocketHandler): def open(self, *args, **kwargs):
"""
客户端和服务端已经建立连接
1. 连接
2. 握手
:param args:
:param kwargs:
:return:
"""
users.add(self) def on_message(self, message):
content = self.render_string('message.html',msg=message)
for client in users:
client.write_message(content) def on_close(self):
"""
客户端主动关闭连接
:return:
"""
users.remove(self) def run():
settings = {
'template_path': 'templates',
'static_path': 'static',
}
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/chat", ChatHandler), ], **settings)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start() if __name__ == "__main__":
run()

通过socket完成接受websocket数据的代码

import socket
import base64
import hashlib def get_headers(data):
"""
将请求头格式化成字典
:param data:
:return:
"""
header_dict = {}
data = str(data, encoding='utf-8') header, body = data.split('\r\n\r\n', 1)
header_list = header.split('\r\n')
for i in range(0, len(header_list)):
if i == 0:
if len(header_list[i].split(' ')) == 3:
header_dict['method'], header_dict['url'], header_dict['protocol'] = header_list[i].split(' ')
else:
k, v = header_list[i].split(':', 1)
header_dict[k] = v.strip()
return header_dict def send_msg(conn, msg_bytes):
import struct token = b"\x81"
length = len(msg_bytes)
if length < 126:
token += struct.pack("B", length)
elif length <= 0xFFFF:
token += struct.pack("!BH", 126, length)
else:
token += struct.pack("!BQ", 127, length) msg = token + msg_bytes
conn.send(msg)
return True sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', 8002))
sock.listen(5) # 等待用户连接
conn, address = sock.accept()
# [握手消息]
data = conn.recv(8096)
headers = get_headers(data)
# for k,v in headers.items():
# print(k,v)
magic_string = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
value = headers['Sec-WebSocket-Key'] + magic_string
ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest()) response_tpl = "HTTP/1.1 101 Switching Protocols\r\n" \
"Upgrade:websocket\r\n" \
"Connection:Upgrade\r\n" \
"Sec-WebSocket-Accept:%s\r\n" \
"WebSocket-Location:ws://%s%s\r\n\r\n"
# 获取[握手消息],magic string,sha1加密
# 发送会客户端
response_str = response_tpl % (ac.decode('utf-8'), headers['Host'], headers['url'])
conn.send(bytes(response_str, encoding='utf-8')) while True:
info = conn.recv(8096)
payload_len = info[1] & 127
if payload_len == 126:
extend_payload_len = info[2:4]
mask = info[4:8]
decoded = info[8:]
elif payload_len == 127:
extend_payload_len = info[2:10]
mask = info[10:14]
decoded = info[14:]
else:
extend_payload_len = None
mask = info[2:6]
decoded = info[6:] bytes_list = bytearray()
for i in range(len(decoded)):
chunk = decoded[i] ^ mask[i % 4]
bytes_list.append(chunk)
body = str(bytes_list, encoding='utf-8') send_msg(conn,bytes(body,encoding='utf-8'))

对于具体的详解请看:https://www.cnblogs.com/wupeiqi/p/6558766.html