Dataplicity Lomond
Tranquil WebSockets for Python.
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