websockets
is a Python package that creates socket servers and clients to allow communication between the two parties; it is a coroutine-based API built on top of Python’s standard asynchronous I/O framework, asyncio
.
The send
and recv
methods of the Python class are the only minimal methods required to perform basic communication. Take a look at the example below:
import asyncioimport websockets# async function to connect to a specified uriasync def intro(uri):async with websockets.connect(uri) as ws:# sending a message to the socket connected to the uriawait ws.send("I am Hassan.")# receiving a message from the connected socketawait ws.recv()asyncio.get_event_loop().run_until_complete(intro('ws://127.0.0.1:8000'))
For more information on Python websockets, visit the official documentation.
Free Resources