Library for building WebSocket servers and clients in Python

websockets

websockets is a library for building WebSocket servers and clients in Python with a focus on correctness and simplicity.

Built on top of asyncio, Python’s standard asynchronous I/O framework, it provides an elegant coroutine-based API.

Here’s how a client sends and receives messages:

#!/usr/bin/env python

import asyncio
from websockets import connect

async def hello(uri):
    async with connect(uri) as websocket:
        await websocket.send("Hello world!")
        await websocket.recv()

asyncio.run(hello("ws://localhost:8765"))

And here’s an echo server:

#!/usr/bin/env python

import asyncio
from websockets import serve

async def echo(websocket, path):
    async for message in websocket:
        await websocket.send(message)

async def main():
    async with serve(echo, "localhost", 8765):
        await asyncio.Future()  # run forever

asyncio.run(main())

Does that look good?

GitHub

https://github.com/aaugustin/websockets

 

 

 

To finish reading, please visit source site