Python写一个简单的websocket server
时间: 2024-03-21 19:16:15 浏览: 170
Sure, you can use the `websockets` library in Python to create a simple WebSocket server. Here's an example:
```python
import asyncio
import websockets
async def echo(websocket, path):
# Receives and echoes back messages from the client
async for message in websocket:
await websocket.send(message)
async def main():
async with websockets.serve(echo, "localhost", 8765):
print("WebSocket server running")
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
```
This code listens on `localhost:8765` and echoes back any messages sent by clients. You can connect to it using a WebSocket client of your choice. Hope this helps!
阅读全文