글로블글http http http http썬이썬。 웹소켓。 구현。
Python源代码
#######################
## WebSocket服务器
#######################
导入异步
导入websockets
#回调websockets.serve(xx,
异步def my_accept(websocket,路径):
而True:
data_rcv =等待websocket.recv(); #从客户端接收数据。
print(“ received data =” + data_rcv);
等待websocket.send(“ websock_svr send data =” + data_rcv); #发送接收到的数据
#websocket服务器创建
websoc_svr = websockets.serve(my_accept,“ localhost”,3000);
print(“这是WebSocket服务器:等待客户端访问”);
#等待
asyncio.get_event_loop()。run_until_complete(websoc_svr);
asyncio.get_event_loop()。run_forever();
#######################
## WebSocket客户端
#######################
导入异步
导入websockets
异步def my_connect():
与websockets.connect(“ ws:// localhost:3000”)作为websocket异步:
对于范围(1,100,1)中的i:
等待websocket.send(“嗨,服务器。我是客户端”);
data_rcv =等待websocket.recv();
#print(“从服务器接收的数据:” + data_rcv);
#连接到服务器
asyncio.get_event_loop()。run_until_complete(my_connect()); 。