simple-websockets
Simple websocket server and client with callback-based API.
Table of Contents
Installation
pip install simple-websockets
Usage
Server
Here is a example of websocket echo server:
from simplews import SimpleWSServer
server = SimpleWSServer(host="",
port=8765,
print_details=True, # Print basic logs
print_messages=True) # Print every message received
@server.on_connect
async def on_connect(ws): # called when a client connects
print(f'Client {ws.remote_address} connected')
@server.on_message
async def on_message(ws, message): # called when a message is received from a client
await ws.send(message) # send the message back to the client
@server.on_close
async def on_close(ws): # called when a client disconnects
print(f'Client {ws.remote_address} disconnected')
server.serve() # serve forever
Client
Here is a example of websocket client:
from simplews import SimpleWSClient
client = SimpleWSClient(uri="ws://localhost:8765",
print_details=True, # Print basic logs
print_messages=True) # Print every message received
@client.on_connect
async def on_connect(ws): # called when the connection is established
print(f'Connected to {ws.remote_address}')
@client.on_message
async def on_message(ws, message): # called when a message is received
print(f'Received: {message}')
@client.on_close
async def on_close(ws): # called when the connection is closed
print(f'Connection to {ws.remote_address} closed')
client.connect() # connect to the server
Tips
- All callbacks are asynchronous, so don't forget
async. - ws.send() is a coroutine, so don't forget
await. - For more information about the
wsargument, see WebSocketServerProtocol for server and WebSocketClientProtocol for client. - server.broadcast() is the preferred way to send a message to all connected clients, and it's not a coroutine, so no
awaitis needed.
License
simple-websockets is distributed under the terms of the GPL-3.0-only license.
Release files for simple-websockets 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| simple_websockets-0.1.0.tar.gz | 14.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_websockets-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.9 kB
Release files / simple_websockets-0.1.0.tar.gz
| Download URL | simple_websockets-0.1.0.tar.gz |
|---|---|
| Size | 14.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6741ee97fb0923049b2f44d98ed5c4681e4e3245551ee27ca13e14b71f5b57a9
|
|
BLAKE2b-256 checksum How to use checksums |
c68a0a22719a88080dde4576a6ec4eb4a5944775192126ef37e8626479dd1ad0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.8
|
Release files / simple_websockets-0.1.0-py3-none-any.whl
| Download URL | simple_websockets-0.1.0-py3-none-any.whl |
|---|---|
| Size | 16.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0942694373aed469470dc0e2f73971276617cfb9c5c790aa1c15328b76924943
|
|
BLAKE2b-256 checksum How to use checksums |
72ae13c83fd35b27d7a2e41212df25ff96f2d7d3070472a593694f2461d4cae0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.8
|