Skip to content

WebSocket library written in Python without dependencies. Supports TLS-protocol.

License

Notifications You must be signed in to change notification settings

arseniyturin/python-websocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python WebSockets from Scratch (WIP)

Threaded WebSocket Server that is very compact, has no external dependencies and can handle text messages

Example: Chat Application

Chat

  • In terminal, type $ make. This will install wssocket package into your environment, run tests and start a local WebSocket server
  • Open examples/index.html on two different tabs
  • Type anything in the chat

API

WebSocket server has several API calls that you can use to control message delivery. At this point only text messages are supported.

Method Description
send(client,message) Send text message to the client
sendall(message) Send text message to all clients
onmessage(callback) New message, user-defined callback function must take two arguments: client and message
onopen(callback) Client connected, user-defined callback function must take one argument: client
onclose(callback) Client left, user-defined callback function must take one argument: client
close(client) Kick client out
shutdown() Shutdown server
clients List all current clients on the server where each client is a socket

Basic Server

from ws import WSServer

# By default server will start on port 8000
# and will accept connections from any host
# Optionally, you can use secure TLS-connection
server = WSServer(host="", port=8000, cert=None, key=None)

# User defined callback function for handling incoming messages
# It must take two arguments:
#  - client, which is a socket that came from user
#  - message, already decoded in user-friendly utf-8
def incomimg_message(client, message):
    # Send text message to all clients
    server.sendall(message)

# Register callback function that will react everytime
# server got new message
server.onmessage(incoming_message)

# Start server, takes no arguments
server.run()

Tests

To run tests:

  • $ python -m unittest -v

Build

To build wheel locally to the ./dist folder, run following:

  • $ python -m pip install --upgrade build
  • $ python -m build

Requirements

  • Python 3.8+

TODO

  • Handle binary data
  • Exit client gracefully
  • Handle errors
  • Handle Cross-Origin Resource Sharing (CORS)

References

About

WebSocket library written in Python without dependencies. Supports TLS-protocol.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published