-2 javascript python sockets websocket
所以基本上我想使用 Python 创建一个 Websocket 服务器,在我的最后一个问题上,我说我不想使用外部库,但这并没有得到很好的接受。那么如何使用外部库来做到这一点呢?我已经弄清楚使用 Javascript 发送 Websocket,但我需要一个 Python Websocket 服务器。那么有人能给我一个 Python 3.6 的工作示例吗?
以下内容来自https://websockets.readthedocs.io/en/stable/intro.html ...这是我在谷歌搜索“python3 websockets”时发现的第二个链接
#!/usr/bin/env python
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print("< {}".format(name))
greeting = "Hello {}!".format(name)
await websocket.send(greeting)
print("> {}".format(greeting))
start_server = websockets.serve(hello, 'localhost', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7648 次 |
| 最近记录: |