赞
踩
【Python】基于asyncio的websocket服务器
发布时间:2018-07-30 17:53,
浏览次数:537
, 标签:
Python
asyncio
websocket
机器环境
* win10
* python3.6
* pip install ws4py
代码
* ws_server.py import asyncio from ws4py.server.tulipserver import * from
ws4py.async_websocketimport WebSocket loop = asyncio.get_event_loop() class
WsServer(WebSocket): clients = set() def opened(self): print("new connection..."
) self.clients.add(self)def received_message(self, m): print("send msg...") for
clientin self.clients: client.send(m) def closed(self, code, reason="Error"):
print("connect close...") def init_server(host, port): factory = lambda:
WebSocketProtocol(WsServer)return loop.create_server(factory, host, port) def
main(): ws_server = loop.run_until_complete(init_server('localhost', 9007))
print('serving on', ws_server.sockets[0].getsockname()) loop.run_forever() if
__name__ =='__main__': main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。