simple_ws
Simple websocket implementation in python
Install
$ pip install simple_ws
Usage
To test the library, clone repo, open two command windows and cd into the python-WS directory
Run python -m http.server 8000
Run python ws_example.py in the other window
Open http://localhost:8000 in a browser
Example
from simple_ws import WebSocket
class WSHandler(WebSocket):
def on_message(self, msg, target_client):
for client in self.clients:
if client.is_open():
client.write_message(msg)
def on_open(self, client):
print("Client connected!")
def on_close(self, client):
print("Client left...")
def on_ping(self, client):
print("Recieved ping!")
def on_pong(self, client):
print("Recieved pong!")
host = ''
port = 8080
ws = WSHandler(host, port)
WebSocket parameters
parameter |
type |
default |
descriptio n |
|---|---|---|---|
host |
String |
Host domain |
|
port |
Integer |
Port number for websocket |
|
ping |
Boolean |
True |
Whether server should ping client in a given intervall, will close connection if pong is not received |
ping_int ervall |
Integer |
5 |
How often should server ping client in seconds, has no effect if ping is set to false |
compress ion |
Boolean |
True |
Whether messages should be compressed |
max_fram e_size |
Integer |
8192 |
Max size for a single websocket frame. If payload exceeds limit, the message will be split in several parts |
buffer_s ize |
Integer |
4096 |
Max network buffer size |
Functions
WebSocket
on_open(self, client)
Called when the server opens a connection to a new client (client).
def on_open(self, client):
# Executes when opening a connection
on_close(self, client)
Called when the server closes a connection to a client (client).
def on_close(self, client):
# Executes when closing a connection
on_message(self, msg, client)
Called when the server has received a message (msg) from a client (client). The message can be in either binary or text format.
def on_message(self, msg, client):
# Executes when server recieves a messages from client
on_ping(self, client)
Called when the server sends a ping to a client (client).
def on_ping(self, client):
# Executes when ping is sent to a client
on_pong(self, client)
Called when the server receives a pong from a client (client).
def on_pong(self, client):
# Executes when pong is received from a client
Client
write_message(self, msg, binary=False)
Sends a message payload (msg) to the client. If binary=True, the message gets sent as binary data.
# Text message
client.write_message("Hello world")
# Binary message
client.write_message(b"0x00", binary=True)
is_open(self)
Returns True if the connection has gone through handshake, and is currently open.
close(self, status, reason)
Sends a close frame to the client, and closes the connection after either a response, or after 1 second. Status and reason are not currently implemented. Will ultimately result in WebSocket.on_close being fired.
client.close(1002, "Pong not recieved")
TODO
Write more tests
Add support for payload in ping and pong frames
Error handling
Clean up classes
Implement close status and reason
Implement all compression configurations
Add more configurability/remove hardcoded constants
Implement connection limit
External sources
License
Metadata
Release files for simple_ws 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simple_ws-0.3.1.tar.gz | 7.4 kB | Without provenance | Details for simple_ws-0.3.1.tar.gz |
Release files / simple_ws-0.3.1.tar.gz
| Download URL | simple_ws-0.3.1.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ddd1afe70c4c0859f60c805241fc6993ee72fa7efb3d1d3cc52168b64a1b213
|
|
BLAKE2b-256 checksum How to use checksums |
d9f24aa9c27788ab90e24a44576f66afe9f8168efbaa6d28fc3a3ffc2074fc0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for simple_ws 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.