Skip to main content

Simple websocket server and client with callback-based API.

Project description

simple-websockets

Simple websocket server and client with callback-based API.

PyPI - Version PyPI - Python Version


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

License

simple-websockets is distributed under the terms of the GPL-3.0-only license.

Project details


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.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

simple_websockets-0.0.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file simple_websockets-0.0.1.tar.gz.

File metadata

  • Download URL: simple_websockets-0.0.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for simple_websockets-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fb6ed9e243f93e777a38ed21146b8ef913ac6e64b56ea9519afb9e6ac8f82113
MD5 77d6754836752bbf03d19317d2183393
BLAKE2b-256 4d9d20b62e7e4ce4f36c47895b50d086b125d4ad9bd5c8bcf95cf18d4df422e6

See more details on using hashes here.

File details

Details for the file simple_websockets-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_websockets-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 12f331eb53d9d3acdc29611014190ba390ccd9096fa0cd8ac0d83a3aa4a7ee99
MD5 3aa642fb5a47b7fd134703c0311d9324
BLAKE2b-256 bff038479750065b2050620997b7c5a6c6578bf26ee880ce95d10438f71913ca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page