Simple websocket server and client with callback-based API.
Project description
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
ws
argument, 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
await
is needed.
License
simple-websockets
is distributed under the terms of the GPL-3.0-only license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
simple_websockets-0.0.2.tar.gz
(14.9 kB
view details)
Built Distribution
File details
Details for the file simple_websockets-0.0.2.tar.gz
.
File metadata
- Download URL: simple_websockets-0.0.2.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0086279cb1e3e48ca4d786861219ece778c2839e5af3c507ea5178a1d850079 |
|
MD5 | 3486d9f3b9ca8994640928d1fb41090e |
|
BLAKE2b-256 | 5b8fc1d51c3c5dd0a0d524c10471e9f78b37402d377a9992ddbe47995326c88d |
File details
Details for the file simple_websockets-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: simple_websockets-0.0.2-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e11e11f95362c4d578774ac114c7747865725228e0d80aec3649f19e0ef4e110 |
|
MD5 | b20000bb0d7eee5172fd67521b65dbe3 |
|
BLAKE2b-256 | e614b9ac030c5026777348ab08f8910d44b2342378f008d57270546d5b53229c |