Code Monkey home page Code Monkey logo

wsproto's Introduction

Pure Python, pure state-machine WebSocket implementation

Build status Documentation Status Code coverage

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 2.7 and 3.5 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 wsaccel is installed (optional), then it will be used to speed things up.

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.

wsproto's People

Contributors

jeamland avatar pgjones avatar kriechi avatar njsmith avatar requires avatar lukasa avatar mehaase avatar agronholm avatar cjerdonek avatar touilleman avatar miracle2k avatar

Watchers

Matthias Urlichs avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.