1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
########
Examples
########
This page needs help! Please see the :ref:`contributing` page to make it better!
Creating Your First WebSocket Connection
==========================================
If you want to connect to a websocket without writing any code yourself, you can
try out the :ref:`getting started` wsdump.py script and the
`examples/ <https://github.com/websocket-client/websocket-client/tree/master/examples>`_
directory files.
You can create your first custom connection with this library using one of the
simple examples below. Note that the first WebSocket example is best for a
short-lived connection, while the WebSocketApp example is best for a long-lived
connection.
**WebSocket example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org")
ws.send("Hello, Server")
print(ws.recv())
ws.close()
**WebSocketApp example**
::
import websocket
def on_message(wsapp, message):
print(message)
wsapp = websocket.WebSocketApp("wss://stream.meetup.com/2/rsvps", on_message=on_message)
wsapp.run_forever()
Debug and Logging Options
==========================
When you're first writing your code, you will want to make sure everything is
working as you planned. The easiest way to view the verbose connection
information is the use ``websocket.enableTrace(True)``. For example, the
following example shows how you can verify that the proper Origin header is set.
.. code-block:: python
:emphasize-lines: 3
import websocket
websocket.enableTrace(True)
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org", origin="testing_websockets.com")
ws.send("Hello, Server")
print(ws.recv())
ws.close()
The output you will see will look something like this:
::
--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Host: echo.websocket.org
Origin: testing123.com
Sec-WebSocket-Key: k9kFAUWNAMmf5OEMfTlOEA==
Sec-WebSocket-Version: 13
Connection: Upgrade
-----------------------
--- response header ---
HTTP/1.1 101 Web Socket Protocol Handshake
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Headers: authorization
Access-Control-Allow-Headers: x-websocket-extensions
Access-Control-Allow-Headers: x-websocket-version
Access-Control-Allow-Headers: x-websocket-protocol
Access-Control-Allow-Origin: testing123.com
Connection: Upgrade
Date: Sat, 06 Feb 2021 12:34:56 GMT
Sec-WebSocket-Accept: 4hNxSu7OllvQZJ43LGpQTuR8+QA=
Server: Kaazing Gateway
Upgrade: websocket
-----------------------
send: b'\x81\x8dS\xfb\xc3a\x1b\x9e\xaf\r<\xd7\xe326\x89\xb5\x04!'
Hello, Server
send: b'\x88\x82 \xc3\x85E#+'
Connection Options
===================
After you can establish a basic WebSocket connection, customizing your
connection using specific options is the next step. Fortunately, this library
provides many options you can configure, such as:
* "Host" header value
* "Cookie" header value
* "Origin" header value
* WebSocket subprotocols
* Custom headers
* SSL or hostname verification
* Timeout value
For a more detailed list of the options available for the different connection
methods, check out the source code comments for each:
* `WebSocket().connect() option docs <https://websocket-client.readthedocs.io/en/latest/core.html#websocket._core.WebSocket.connect>`_
* Related: `WebSocket.create_connection() option docs <https://websocket-client.readthedocs.io/en/latest/core.html#websocket._core.create_connection>`_
* `WebSocketApp() option docs <https://websocket-client.readthedocs.io/en/latest/app.html#websocket._app.WebSocketApp.__init__>`_
* Related: `WebSocketApp.run_forever docs <https://websocket-client.readthedocs.io/en/latest/app.html#websocket._app.WebSocketApp.run_forever>`_
Setting Common Header Values
--------------------------------
To modify the ``Host``, ``Origin``, ``Cookie``, or ``Sec-WebSocket-Protocol``
header values of the WebSocket handshake request, pass the ``host``, ``origin``,
``cookie``, or ``subprotocols`` options to your WebSocket connection. The first
two examples show the Host, Origin, and Cookies headers being set, while the
Sec-WebSocket-Protocol header is set separately in the following example.
For debugging, remember that it is helpful to enable :ref:`Debug and Logging Options`.
**WebSocket common headers example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org", cookie="chocolate",
origin="testing_websockets.com", host="echo.websocket.org/websocket-client-test")
**WebSocketApp common headers example**
::
import websocket
def on_message(wsapp, message):
print(message)
wsapp = websocket.WebSocketApp("wss://stream.meetup.com/2/rsvps",
cookie="chocolate", on_message=on_message)
wsapp.run_forever(origin="testing_websockets.com", host="127.0.0.1")
**WebSocket subprotocols example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("wss://ws.kraken.com", subprotocols=["testproto"])
**WebSocketApp subprotocols example**
::
import websocket
def on_message(wsapp, message):
print(message)
wsapp = websocket.WebSocketApp("wss://ws.kraken.com",
subprotocols=["testproto"], on_message=on_message)
wsapp.run_forever()
Suppress Origin Header
-------------------------
There is a special ``suppress_origin`` option that can be used to remove the
``Origin`` header from connection handshake requests. The below examples
illustrate how this can be used.
For debugging, remember that it is helpful to enable :ref:`Debug and Logging Options`.
**WebSocket suppress origin example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org", suppress_origin=True)
**WebSocketApp suppress origin example**
::
import websocket
def on_message(wsapp, message):
print(message)
wsapp = websocket.WebSocketApp("wss://stream.meetup.com/2/rsvps",
on_message=on_message)
wsapp.run_forever(suppress_origin=True)
Setting Custom Header Values
--------------------------------
Setting custom header values, other than ``Host``, ``Origin``, ``Cookie``, or
``Sec-WebSocket-Protocol`` (which are addressed above), in the WebSocket
handshake request is similar to setting common header values. Use the ``header``
option to provide custom header values in a list or dict.
For debugging, remember that it is helpful to enable :ref:`Debug and Logging Options`.
**WebSocket custom headers example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org",
header={"CustomHeader1":"123", "NewHeader2":"Test"})
**WebSocketApp custom headers example**
::
import websocket
def on_message(wsapp, message):
print(message)
wsapp = websocket.WebSocketApp("wss://stream.meetup.com/2/rsvps",
header={"CustomHeader1":"123", "NewHeader2":"Test"}, on_message=on_message)
wsapp.run_forever()
Disabling SSL or Hostname Verification
---------------------------------------
See the relevant :ref:`FAQ` page for instructions.
Using a Custom Class
--------------------------------
You can also write your own class for the connection, if you want to handle
the nitty-gritty connection details yourself.
::
import socket
from websocket import create_connection, WebSocket
class MyWebSocket(WebSocket):
def recv_frame(self):
frame = super().recv_frame()
print('yay! I got this frame: ', frame)
return frame
ws = create_connection("ws://echo.websocket.org/",
sockopt=((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),), class_=MyWebSocket)
Setting Timeout Value
--------------------------------
The _socket.py file contains the functions ``setdefaulttimeout()`` and
``getdefaulttimeout()``. These two functions set the global ``_default_timeout``
value, which sets the socket timeout value (in seconds). These two functions
should not be confused with the similarly named ``settimeout()`` and
``gettimeout()`` functions found in the _core.py file. With WebSocketApp, the
``run_forever()`` function gets assigned the timeout `from getdefaulttimeout()
<https://github.com/websocket-client/websocket-client/blob/29c15714ac9f5272e1adefc9c99b83420b409f63/websocket/_app.py#L248>`_.
When the timeout value is reached, the exception WebSocketTimeoutException is
triggered by the _socket.py ``send()`` and ``recv()`` functions. Additional timeout
values can be found in other locations in this library,
including the ``close()`` function of the WebSocket class and the
``create_connection()`` function of the WebSocket class.
The WebSocket timeout example below shows how an exception is triggered after
no response is received from the server after 5 seconds.
**WebSocket timeout example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org", timeout=5)
#ws.send("Hello, Server") # Commented out to trigger WebSocketTimeoutException
print(ws.recv())
# Program should end with a WebSocketTimeoutException
The WebSocketApp timeout example works a bit differently than the WebSocket
example. Because WebSocketApp handles long-lived connections, it does not
timeout after a certain amount of time without receiving a message. Instead, a
timeout is triggered if no connection response is received from the server after
the timeout interval (5 seconds in the example below).
**WebSocketApp timeout example**
::
import websocket
def on_error(wsapp, err):
print("Got a an error: ", err)
websocket.setdefaulttimeout(5)
wsapp = websocket.WebSocketApp("ws://nexus-websocket-a.intercom.io",
on_error=on_error)
wsapp.run_forever()
# Program should print a "timed out" error message
Connecting through a HTTP proxy
--------------------------------
The example below show how to connect through a HTTP proxy. Note that this
library does support authentication to a proxy using the ``http_proxy_auth``
parameter. Be aware that the current implementation of websocket-client uses
the "CONNECT" method, and the proxy server must allow the "CONNECT" method. For
example, the squid proxy only allows the "CONNECT" method
`on HTTPS ports <https://wiki.squid-cache.org/Features/HTTPS#CONNECT_tunnel>`_
by default.
**WebSocket proxy example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org",
http_proxy_host="127.0.0.1", http_proxy_port="8080")
ws.send("Hello, Server")
print(ws.recv())
ws.close()
**WebSocketApp proxy example**
`Work in progress - coming soon`
Using Unix Domain Sockets
--------------------------------
You can also connect to a WebSocket server hosted on a unix domain socket.
Just use the ``socket`` option when creating your connection.
::
import socket
from websocket import create_connection
my_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
my_socket.connect("/path/to/my/unix.socket")
ws = create_connection("ws://localhost/", # Dummy URL
socket = my_socket,
sockopt=((socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),))
Post-connection features
==========================
You can see a summary of this library's supported WebSocket features by either
running the autobahn test suite against this client, or by reviewing a recently
run autobahn report, available as a .html file in the /compliance directory.
Ping/Pong Usage
--------------------------------
The WebSocket specification defines
`ping <https://tools.ietf.org/html/rfc6455#section-5.5.2>`_ and
`pong <https://tools.ietf.org/html/rfc6455#section-5.5.3>`_
message opcodes as part of the protocol. These can serve as a way to keep a
long-lived connection active even if data is not being transmitted. However, if
a blocking event is happening, there may be some issues with ping/pong. The
below examples demonstrate how ping and pong can be sent by this library. You
can get additional debugging information by using
`Wireshark <https://www.wireshark.org/>`_
to view the ping and pong messages being sent. In order for Wireshark to
identify the WebSocket protocol properly, it should observe the initial HTTP
handshake and the HTTP 101 response in cleartext (without encryption) -
otherwise the WebSocket messages may be categorized as TCP or TLS messages.
For debugging, remember that it is helpful to enable :ref:`Debug and Logging Options`.
**WebSocket ping/pong example**
This example is best for a quick test where you want to check the effect of a
ping, or where situations where you want to customize when the ping is sent.
This type of connection does not automatically respond to a "ping" with a "pong".
::
import websocket
websocket.enableTrace(True)
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.org")
ws.ping()
ws.pong()
ws.close()
**WebSocketApp ping/pong example**
This example, and ``run_forever()`` in general, is better for long-lived connections.
If a server needs a regular ping to keep the connection alive, this is probably
the option you will want to use. The ``run_forever()`` function will automatically
send a "pong" when it receives a "ping", per the specification.
::
import websocket
def on_message(wsapp, message):
print(message)
def on_ping(wsapp, message):
print("Got a ping!")
def on_pong(wsapp, message):
print("Got a pong! No need to respond")
wsapp = websocket.WebSocketApp("wss://stream.meetup.com/2/rsvps",
on_message=on_message, on_ping=on_ping, on_pong=on_pong)
wsapp.run_forever(ping_interval=10, ping_timeout=60)
Real-world Examples
=========================
Other projects that depend on websocket-client may be able to illustrate more
complex use cases for this library. A few of the projects using websocket-client
are listed below:
- `https://github.com/slackapi/python-slack-sdk <https://github.com/slackapi/python-slack-sdk>`_
- `https://github.com/wee-slack/wee-slack <https://github.com/wee-slack/wee-slack>`_
- `https://github.com/aluzzardi/wssh/ <https://github.com/aluzzardi/wssh/>`_
- `https://github.com/invisibleroads/socketIO-client <https://github.com/invisibleroads/socketIO-client>`_
- `https://github.com/Ape/samsungctl <https://github.com/Ape/samsungctl>`_
- `https://github.com/xchwarze/samsung-tv-ws-api <https://github.com/xchwarze/samsung-tv-ws-api>`_
- `https://github.com/andresriancho/websocket-fuzzer <https://github.com/andresriancho/websocket-fuzzer>`_
|