python3 实现多人websocket聊天室
2021/10/9 11:38:40
本文主要是介绍python3 实现多人websocket聊天室,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python 版本:python3.7.4
前端网页
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <div> <h3>昵称:<input type="text" id="user"/></h3> <input type="text" value="说:" id="say" hidden/> <h3>发送消息:</h3><input type="text" id="txt" style="height: 100px;width: 200px"/><br> <input type="button" id="btn" value="提交" style="height: 30px" onclick="sendMsg();"/> <input type="reset" value="reset" onclick=location="websocket.html"> <input type="button" id="close" value="关闭连接" onclick="closeConn();"/> </div> <div id="content"></div> <script type="text/javascript"> var socket = new WebSocket("ws://127.0.0.1:9999/chatsocket"); socket.onopen = function () { var newTag = document.createElement('div'); newTag.innerHTML = "【连接成功】"; document.getElementById('content').appendChild(newTag); }; socket.onmessage = function (event) { var response = event.data; var newTag = document.createElement('div'); newTag.innerHTML = response; document.getElementById('content').appendChild(newTag); }; socket.onclose = function (event) { var newTag = document.createElement('div'); newTag.innerHTML = "【连接失败】"; document.getElementById('content').appendChild(newTag); }; function sendMsg() { var txt = document.getElementById('txt'); var user = document.getElementById('user'); var say = document.getElementById('say'); socket.send(user.value+say.value+txt.value); txt.value = ""; } function closeConn() { socket.close(); var newTag = document.createElement('div'); newTag.innerHTML = "【关闭连接】"; document.getElementById('content').appendChild(newTag); } </script> </body> </html>
后端python代码:
from socket import * import socket import base64 import hashlib import threading def get_headers(data): 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 class Chat: def __init__(self,ip='0.0.0.0',port=9999): self.addr=(ip,port) self.sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.client= {} def start(self,): self.sock.bind((self.addr)) self.sock.listen() threading.Thread(target=self.accept,name='accept').start() def accept(self): while True: conn, address = self.sock.accept() self.client[address] = conn data = conn.recv(1024) headers = get_headers(data) 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 = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' value = headers['Sec-WebSocket-Key'] + magic_string ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest()) response_str = response_tpl % (ac.decode('utf-8'), headers['Host'], headers['url']) conn.send(bytes(response_str, encoding='utf-8')) threading.Thread(target=self.recv,name='sock',args=(conn,)).start() def recv(self,sock): while 1: try: info = sock.recv(8096) except Exception as e: info = None if not info: break 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') for s in self.client.values(): send_msg(s, bytes(body, encoding='utf-8')) def stop(self): for s in self.client.values(): s.close() self.sock.close() sc=Chat() sc.start()
使用9999端口,启用脚本时确保端口没有被占用
测试:
这篇关于python3 实现多人websocket聊天室的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Python基础编程
- 2024-11-25Python编程基础:变量与类型
- 2024-11-25Python编程基础与实践
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南