python websockets模块
https://websockets.readthedocs.io/en/stable/intro/tutorial1.html
django插件 dwebsocket
https://github.com/duanhongyi/dwebsocket/blob/master/dwebsocket/factory.py
pywss实现类似与flask框架
pywss是一种类似Flask开发的WebSocket-Server服务端框架,适用python3.X
pip install pywss
from pywss import Pyws, route
@route('/websocket')
def my_websocket(request, data):
print(‘客户端发来数据:%s’ % data)
return data + ' - data from pywss'
if __name__ == '__main__':
ws = Pyws(__name__, address='127.0.0.1', port=8888)
ws.serve_forever()
html:
<!DOCTYPE html>
<html>
<head>
<title>django-websocket</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">//<![CDATA[
$(function () {
$('#connect_websocket').click(function () {
if (window.s) {
window.s.close()
}
/*创建socket连接*/
var socket = new WebSocket("ws://127.0.0.1:8888/websocket");
socket.onopen = function () {
console.log('WebSocket open');//成功连接上Websocket
};
socket.onmessage = function (e) {
console.log('message: ' + e.data);//打印出服务端返回过来的数据
$('#messagecontainer').prepend('<p>' + e.data + '</p>');
};
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();
window.s = socket;
});
$('#send_message').click(function () {
//如果未连接到websocket
if (!window.s) {
alert("websocket未连接.");
} else {
window.s.send($('#message').val());//通过websocket发送数据
}
});
$('#close_websocket').click(function () {
if (window.s) {
window.s.close();//关闭websocket
console.log('websocket已关闭');
}
});
});
//]]></script>
</head>
<body>
<br>
<input type="text" id="message" value="Hello, World!"/>
<button type="button" id="connect_websocket">连接 websocket</button>
<button type="button" id="send_message">发送 message</button>
<button type="button" id="close_websocket">关闭 websocket</button>
<h1>Received Messages</h1>
<div id="messagecontainer">
</div>
</body>
</html>
示例2:
示例三
https://blog.csdn.net/u012751272/article/details/89197325?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162692376716780274171185%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162692376716780274171185&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-6-89197325.first_rank_v2_pc_rank_v29&utm_term=dwebsocket+%E8%81%8A%E5%A4%A9&spm=1018.2226.3001.4187
部署问题
https://blog.csdn.net/weixin_44784018/article/details/103872287?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162692376716780274171185%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162692376716780274171185&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-7-103872287.first_rank_v2_pc_rank_v29&utm_term=dwebsocket+%E8%81%8A%E5%A4%A9&spm=1018.2226.3001.4187
这样写
1.
from dwebsocket.decorators import accept_websocket
data = []
name_list = ['1号选手', '2号选手', '3号选手']
@accept_websocket
def ws(request):
if request.is_websocket():
# name = {name_list[len(data)]: request.websocket}
data.append(request.websocket)
print(data)
while 1:
msg = request.websocket.wait()
if msg:
for k in data:
k.send(msg)
from dwebsocket.decorators import accept_websocket
data = []
name_list = ['1号选手', '2号选手', '3号选手']
@accept_websocket
def ws(request):
if request.is_websocket():
# name = {name_list[len(data)]: request.websocket}
data.append(request.websocket)
print(data)
for msg in request.websocket:
if msg:
for k in data:
k.send(msg)
python 协程websocket(客户端、服务端)
import asyncio
import websockets
# 服务器端:
async def echo(websocket, path):
async for message in websocket:
await websocket.send(message)
async def main():
async with websockets.serve(echo, "localhost", 8765):
await asyncio.Future()
asyncio.run(main())
# 客户端:
import asyncio
import websockets
async def hello():
async with websockets.connect("ws://localhost:8765") as websocket:
name = input("What's your name? ")
await websocket.send(name)
greeting = await websocket.recv()
print(f"{greeting}")
asyncio.run(hello())