• Stars
    star
    236
  • Rank 169,503 (Top 4 %)
  • Language
    Python
  • License
    Other
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lomond is a Websocket client for Python which turns a websocket connection in to an orderly stream of events. Contrast this with the existing websocket clients available for Python which follow a more JS-like model of threads and callbacks.

Dataplicity Lomond

Tranquil WebSockets for Python.

PyPI version PyPI Coverage Status CircleCI

Lomond is a Websocket client which turns a websocket connection in to an orderly stream of events. No threads or callbacks necessary.

How to Use

To connect to a "ws:" or "wss:" WebSocket URL, construct a lomond.WebSocket object then iterate over it. This will yield an event object for each step in the connection process and for any data sent by the server.

You will receive a Binary or Text event when the server sends you a message. You may send a message with the send_binary or send_text methods.

Example

The following is a silly example that connects to a websocket server (in this case a public echo server), and sends a string of text every 5 seconds.

from lomond import WebSocket


websocket = WebSocket('wss://echo.websocket.org')

for event in websocket:
    if event.name == 'poll':
        websocket.send_text('Hello, World')
    elif event.name == 'text':
        print(event.text)

Events

A successful websocket connection will result in a series of events such as the following:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      Connecting      β”‚     Contacting server
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     Connected to server (but
β”‚      Connected       β”‚     not yet sent data)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     Negotiated Websocket
β”‚        Ready         β”‚     handshake
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚  β”‚           β”‚
           β–Ό  β–Ό           β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  Send and receive
β”‚ Binary / Text / Poll β”‚β”€β”€β”˜  application data
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     Websocket close
β”‚        Closed        β”‚     handshake
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Disconnected     β”‚     Disconnected TCP/IP
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     connection to server