Skip to main content

WebSockets state-machine based protocol implementation

Project description

Build Status Code Coverage Documentation Status Chat community

This repository contains a pure-Python implementation of a WebSocket protocol stack. It’s written from the ground up to be embeddable in whatever program you choose to use, ensuring that you can communicate via WebSockets, as defined in RFC6455, regardless of your programming paradigm.

This repository does not provide a parsing layer, a network layer, or any rules about concurrency. Instead, it’s a purely in-memory solution, defined in terms of data actions and WebSocket frames. RFC6455 and Compression Extensions for WebSocket via RFC7692 are fully supported.

wsproto supports Python 3.6.1 or higher.

To install it, just run:

$ pip install wsproto

Usage

Let’s assume you have some form of network socket available. wsproto client connections automatically generate a HTTP request to initiate the WebSocket handshake. To create a WebSocket client connection:

from wsproto import WSConnection, ConnectionType
from wsproto.events import Request

ws = WSConnection(ConnectionType.CLIENT)
ws.send(Request(host='echo.websocket.org', target='/'))

To create a WebSocket server connection:

from wsproto.connection import WSConnection, ConnectionType

ws = WSConnection(ConnectionType.SERVER)

Every time you send a message, or call a ping, or simply if you receive incoming data, wsproto might respond with some outgoing data that you have to send:

some_socket.send(ws.bytes_to_send())

Both connection types need to receive incoming data:

ws.receive_data(some_byte_string_of_data)

And wsproto will issue events if the data contains any WebSocket messages or state changes:

for event in ws.events():
    if isinstance(event, Request):
        # only client connections get this event
        ws.send(AcceptConnection())
    elif isinstance(event, CloseConnection):
        # guess nobody wants to talk to us any more...
    elif isinstance(event, TextMessage):
        print('We got text!', event.data)
    elif isinstance(event, BytesMessage):
        print('We got bytes!', event.data)

Take a look at our docs for a full list of events <https://wsproto.readthedocs.io/en/latest/api.html#events>!

Testing

It passes the autobahn test suite completely and strictly in both client and server modes and using permessage-deflate.

If you want to run the compliance tests, go into the compliance directory and then to test client mode, in one shell run the Autobahn test server:

$ wstest -m fuzzingserver -s ws-fuzzingserver.json

And in another shell run the test client:

$ python test_client.py

And to test server mode, run the test server:

$ python test_server.py

And in another shell run the Autobahn test client:

$ wstest -m fuzzingclient -s ws-fuzzingclient.json

Documentation

Documentation is available at https://wsproto.readthedocs.io/en/latest/.

Contributing

wsproto welcomes contributions from anyone! Unlike many other projects we are happy to accept cosmetic contributions and small contributions, in addition to large feature requests and changes.

Before you contribute (either by opening an issue or filing a pull request), please read the contribution guidelines.

License

wsproto is made available under the MIT License. For more details, see the LICENSE file in the repository.

Authors

wsproto was created by @jeamland, and is maintained by the python-hyper community.

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

wsproto-1.1.0.tar.gz (52.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wsproto-1.1.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file wsproto-1.1.0.tar.gz.

File metadata

  • Download URL: wsproto-1.1.0.tar.gz
  • Upload date:
  • Size: 52.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

File hashes

Hashes for wsproto-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a2e56bfd5c7cd83c1369d83b5feccd6d37798b74872866e62616e0ecf111bda8
MD5 0be57d8f7431b9a0a345498d61583065
BLAKE2b-256 f558575e416cd024d23c52de9e17d02f4c5ef4a9c8a7258c6fc8f2b3f96f73cf

See more details on using hashes here.

File details

Details for the file wsproto-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: wsproto-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

File hashes

Hashes for wsproto-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2218cb57952d90b9fca325c0dcfb08c3bda93e8fd8070b0a17f048e2e47a521b
MD5 3922b47a149bfc3dc0891d0077c3eeb7
BLAKE2b-256 4b6e5f8c3e8418966f50d028e336f0c2c568f8522577183678923609d4d24924

See more details on using hashes here.

Supported by

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