当前位置:   article > 正文

python3.6 websocket异步高并发_【Python】基于asyncio的websocket服务器

python websocket asyncio

【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()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/85320
推荐阅读
相关标签
  

闽ICP备14008679号