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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
|
########
Examples
########
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**
.. doctest:: sample-connection
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events")
>>> ws.send("Hello, Server")
19
>>> print(ws.recv())
echo.websocket.events sponsored by Lob.com
>>> ws.close()
**WebSocketApp example**
.. doctest:: sample-connection
>>> import websocket
>>> def on_message(wsapp, message):
... print(message)
>>> wsapp = websocket.WebSocketApp("wss://testnet-explorer.binance.org/ws/block", on_message=on_message)
>>> wsapp.run_forever() # doctest: +SKIP
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.events/", 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.events
Origin: testing_websockets.com
Sec-WebSocket-Key: GnuCGEiF3OuyRESXiVnsAQ==
Sec-WebSocket-Version: 13
Connection: Upgrade
-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: websocket
Sec-Websocket-Accept: wvhwrjThsVAyr/V4Hzn5tWMSomI=
Via: 1.1 vegur
-----------------------
++Sent raw: b'\x81\x8d\xd4\xda9\xee\x9c\xbfU\x82\xbb\xf6\x19\xbd\xb1\xa8O\x8b\xa6'
++Sent decoded: fin=1 opcode=1 data=b'Hello, Server'
19
++Rcv raw: b'\x81*echo.websocket.events sponsored by Lob.com'
++Rcv decoded: fin=1 opcode=1 data=b'echo.websocket.events sponsored by Lob.com'
echo.websocket.events sponsored by Lob.com
++Sent raw: b'\x88\x82\xc9\x8c\x14\x99\xcad'
++Sent decoded: fin=1 opcode=8 data=b'\x03\xe8'
Using websocket-client with "with" statements
==============================================
It is possible to use "with" statements, as outlined in PEP 343, to help
manage the closing of WebSocket connections after a message is received.
Below is one example of this being done with a short-lived connection:
**Short-lived WebSocket using "with" statement**
.. doctest:: sample-connection
>>> from contextlib import closing
>>> from websocket import create_connection
>>> with closing(create_connection("wss://testnet-explorer.binance.org/ws/block")) as conn:
... print(conn.recv()) # doctest: +SKIP
# Connection is now closed
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**
.. doctest:: headers
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events", cookie="chocolate",
... origin="testing_websockets-client.com", host="echo.websocket.events")
**WebSocketApp common headers example**
.. doctest:: headers
>>> import websocket
>>> def on_message(wsapp, message):
... print(message)
>>> wsapp = websocket.WebSocketApp("wss://testnet-explorer.binance.org/ws/block",
... cookie="chocolate", on_message=on_message)
>>> wsapp.run_forever(origin="testing_websockets.com", host="127.0.0.1") # doctest: +SKIP
**WebSocket subprotocols example**
Use this to specify STOMP, WAMP, MQTT, or other values of the "Sec-WebSocket-Protocol" header.
Be aware that websocket-client does not include support for these protocols,
so your code must handle the data sent over the WebSocket connection.
.. doctest:: subprotocols
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("wss://ws.kraken.com", subprotocols=["mqtt"])
**WebSocketApp subprotocols example**
.. doctest:: subprotocols
>>> import websocket
>>> def on_message(wsapp, message):
... print(message)
>>> wsapp = websocket.WebSocketApp("wss://ws.kraken.com",
... subprotocols=["STOMP"], on_message=on_message)
>>> wsapp.run_forever() # doctest: +SKIP
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**
.. doctest:: suppress-origin
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events", suppress_origin=True)
**WebSocketApp suppress origin example**
.. doctest:: suppress-origin
>>> import websocket
>>> def on_message(wsapp, message):
... print(message)
>>> wsapp = websocket.WebSocketApp("wss://testnet-explorer.binance.org/ws/block",
... on_message=on_message)
>>> wsapp.run_forever(suppress_origin=True) # doctest: +SKIP
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`.
There is no built-in support for "Sec-WebSocket-Extensions" header values as
defined in RFC 7692.
**WebSocket custom headers example**
.. doctest:: custom-headers
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events",
... header={"CustomHeader1":"123", "NewHeader2":"Test"})
**WebSocketApp custom headers example**
.. doctest:: custom-headers
>>> import websocket
>>> def on_message(wsapp, message):
... print(message)
>>> wsapp = websocket.WebSocketApp("wss://testnet-explorer.binance.org/ws/block",
... header={"CustomHeader1":"123", "NewHeader2":"Test"}, on_message=on_message)
>>> wsapp.run_forever() # doctest: +SKIP
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.
.. doctest:: custom-class
>>> 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.events/",
... 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**
.. doctest:: timeout-example
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events", timeout=5)
>>> # ws.send("Hello, Server") # Commented out to trigger WebSocketTimeoutException
>>> print(ws.recv()) # doctest: +SKIP
# 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**
.. doctest:: timeout-example
>>> import websocket
>>> def on_error(wsapp, err):
... print("EXAMPLE error encountered: ", err)
>>> websocket.setdefaulttimeout(5)
>>> wsapp = websocket.WebSocketApp("ws://nexus-websocket-a.intercom.io",
... on_error=on_error)
>>> wsapp.run_forever() # doctest: +SKIP
# Program should print a "timed out" error message
Connecting through a proxy
----------------------------
websocket-client supports proxied connections. The supported
proxy protocols are HTTP, SOCKS4, SOCKS4a, SOCKS5, and SOCKS5h.
If you want to route DNS requests through the proxy, use SOCKS4a
or SOCKS5h. The proxy protocol should be specified in lowercase to the
``proxy_type`` parameter. The example below shows how to connect through a
HTTP or SOCKS proxy. Proxy authentication is supported with the ``http_proxy_auth``
parameter, which should be a tuple of the username and password. Be aware
that the current implementation of websocket-client uses the "CONNECT"
method for HTTP proxies (though soon the HTTP proxy handling will use
the same ``python_socks`` library currently enabled only for SOCKS proxies),
and the HTTP proxy server must allow the "CONNECT" method. For example,
the squid HTTP proxy only allows the "CONNECT" method
`on HTTPS ports <https://wiki.squid-cache.org/Features/HTTPS#CONNECT_tunnel>`_
by default. You may encounter problems if using SSL/TLS with your proxy.
**WebSocket HTTP proxy with authentication example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.events",
http_proxy_host="127.0.0.1", http_proxy_port="8080",
proxy_type="http", http_proxy_auth=("username", "password123"))
ws.send("Hello, Server")
print(ws.recv())
ws.close()
**WebSocket SOCKS4 (or SOCKS5) proxy example**
::
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.events",
http_proxy_host="192.168.1.18", http_proxy_port="4444", proxy_type="socks4")
ws.send("Hello, Server")
print(ws.recv())
ws.close()
**WebSocketApp proxy example**
::
import websocket
ws = websocket.WebSocketApp("ws://echo.websocket.events")
wsapp.run_forever(proxy_type="socks5", http_proxy_host=proxy_ip, http_proxy_auth=(proxy_username, proxy_password))
Connecting with Custom Sockets
--------------------------------
You can also connect to a WebSocket server hosted on a
specific socket using the ``socket`` option when
creating your connection. Below is an example of using
a unix domain socket.
::
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),))
Other socket types can also be used. The following example
is for a AF_INET (IP address) socket.
::
import socket
from websocket import create_connection
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_socket.bind(("172.18.0.1", 3002))
my_socket.connect()
ws = create_connection("ws://127.0.0.1/", # Dummy URL
socket = my_socket)
Post-connection Feature Summary
-------------------------------
`Autobahn|TestSuite <https://github.com/crossbario/autobahn-testsuite>`_ is an
independent automated test suite to verify the compliance of WebSocket implementations.
Running the test suite against this library will produce a summary report of the
conformant features that have been implemented.
A recently-run autobahn report (available as an .html file) is available 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
connection active even if data is not being transmitted.
Pings may be sent in either direction. If the client receives a ping, a pong
reply will be automatically sent.
However, if a blocking event is happening, there may be some issues with
ping/pong. Below are examples of 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.
.. doctest:: ping-pong
>>> import websocket
>>> websocket.enableTrace(True)
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events")
>>> ws.ping()
>>> ws.ping("This is an optional ping payload")
>>> ws.close()
**WebSocketApp ping/pong example**
This example, and ``run_forever()`` in general, is better for long-lived connections.
In this example, if a ping is received and a pong is sent in response, then the
client is notified via ``on_ping()``.
Further, a ping is transmitted every 60 seconds. If a pong is received, then the client
is notified via ``on_pong()``. If no pong is received within 10 seconds, then
``run_forever()`` will exit with a ``WebSocketTimeoutException``.
.. doctest:: ping-pong
>>> import websocket
>>> def on_message(wsapp, message):
... print(message)
>>> def on_ping(wsapp, message):
... print("Got a ping! A pong reply has already been automatically sent.")
>>> def on_pong(wsapp, message):
... print("Got a pong! No need to respond")
>>> wsapp = websocket.WebSocketApp("wss://testnet-explorer.binance.org/ws/block",
... on_message=on_message, on_ping=on_ping, on_pong=on_pong)
>>> wsapp.run_forever(ping_interval=60, ping_timeout=10, ping_payload="This is an optional ping payload") # doctest: +SKIP
Sending Connection Close Status Codes
--------------------------------------
RFC6455 defines `various status codes <https://tools.ietf.org/html/rfc6455#section-7.4>`_
that can be used to identify the reason for a close frame ending
a connection. These codes are defined in the websocket/_abnf.py
file. To view the code used to close a connection, you can
:ref:`enable logging<Debug and Logging Options>` to view the
status code information. You can also specify your own status code
in the .close() function, as seen in the examples below. Specifying
a custom status code is necessary when using the custom
status code values between 3000-4999.
**WebSocket sending close() status code example**
.. doctest:: close-status
>>> import websocket
>>> websocket.enableTrace(True)
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events")
>>> ws.send("Hello, Server")
19
>>> print(ws.recv())
echo.websocket.events sponsored by Lob.com
>>> ws.close(websocket.STATUS_PROTOCOL_ERROR) # doctest: +SKIP
# Alternatively, use ws.close(status=1002)
**WebSocketApp sending close() status code example**
.. doctest:: close-status
>>> import websocket
>>> websocket.enableTrace(True)
>>> def on_message(wsapp, message):
... print(message)
... wsapp.close(status=websocket.STATUS_PROTOCOL_ERROR) # Alternatively, use wsapp.close(status=1002)
>>> wsapp = websocket.WebSocketApp("wss://testnet-explorer.binance.org/ws/block", on_message=on_message)
>>> wsapp.run_forever(skip_utf8_validation=True) # doctest: +SKIP
Receiving Connection Close Status Codes
-----------------------------------------
The RFC6455 spec states that it is optional for a server to send a
close status code when closing a connection. The RFC refers to these
codes as WebSocket Close Code Numbers, and their meanings are
described in the RFC. It is possible to view
this close code, if it is being sent, to understand why the connection is
being close. One option to view the code is to
:ref:`enable logging<Debug and Logging Options>` to view the
status code information. If you want to use the close status code
in your program, examples are shown below for how to do this.
**WebSocket receiving close status code example**
.. doctest:: close-status
>>> import websocket
>>> import struct
>>> websocket.enableTrace(True)
>>> ws = websocket.WebSocket()
>>> ws.connect("wss://tsock.us1.twilio.com/v3/wsconnect")
>>> ws.send("Hello")
11
>>> resp_opcode, msg = ws.recv_data()
>>> print("Response opcode: " + str(resp_opcode)) # doctest: +SKIP
>>> if resp_opcode == 8 and len(msg) >= 2:
... print("Response close code: " + str(struct.unpack("!H", msg[0:2])[0])) # doctest: +SKIP
... print("Response message: " + str(msg[2:])) # doctest: +SKIP
... else:
... print("Response message: " + str(msg)) # doctest: +SKIP
**WebSocketApp receiving close status code example**
.. doctest:: close-status
>>> import websocket
>>> websocket.enableTrace(True)
>>> def on_close(wsapp, close_status_code, close_msg):
... # Because on_close was triggered, we know the opcode = 8
... print("on_close args:")
... if close_status_code or close_msg:
... print("close status code: " + str(close_status_code))
... print("close message: " + str(close_msg))
>>> def on_open(wsapp):
... wsapp.send("Hello")
>>> wsapp = websocket.WebSocketApp("wss://tsock.us1.twilio.com/v3/wsconnect", on_close=on_close, on_open=on_open)
>>> wsapp.run_forever() # doctest: +SKIP
Customizing frame mask
--------------------------------
WebSocket frames use masking with a random value to add entropy. The masking
value in websocket-client is normally set using os.urandom in the
websocket/_abnf.py file. However, this value can be customized as you wish.
One use case, outlined in
`issue #473 <https://github.com/websocket-client/websocket-client/issues/473>`_,
is to set the masking key to a null value to make it easier to decode the
messages being sent and received. This is effectively the same as "removing" the
mask, though the mask cannot be fully "removed" because it is a part of the
WebSocket frame. Tools such as Wireshark can automatically remove masking
from payloads to decode the payload message, but it may be easier to skip
the demasking step in your custom project.
**WebSocket custom masking key code example**
.. doctest:: frame-mask
>>> import websocket
>>> websocket.enableTrace(True)
>>> def zero_mask_key(_):
... return "\x00\x00\x00\x00"
>>> ws = websocket.WebSocket()
>>> ws.set_mask_key(zero_mask_key)
>>> ws.connect("ws://echo.websocket.events")
>>> ws.send("Hello, Server") # doctest: +SKIP
>>> print(ws.recv()) # doctest: +SKIP
>>> ws.close()
**WebSocketApp custom masking key code example**
.. doctest:: frame-mask
>>> import websocket
>>> websocket.enableTrace(True)
>>> def zero_mask_key(_):
... return "\x00\x00\x00\x00"
>>> def on_message(wsapp, message):
... print(message)
>>> wsapp = websocket.WebSocketApp("wss://testnet-explorer.binance.org/ws/block", on_message=on_message, get_mask_key=zero_mask_key)
>>> wsapp.run_forever() # doctest: +SKIP
Customizing opcode
--------------------------------
WebSocket frames contain an opcode, which defines whether the frame contains
text data, binary data, or is a special frame. The different opcode values
are defined in
`RFC6455 section 11.8 <https://tools.ietf.org/html/rfc6455#section-11.8>`_.
Although the text opcode, 0x01, is the most commonly used value, the
websocket-client library makes it possible to customize which opcode is used.
**WebSocket custom opcode code example**
.. doctest:: custom-opcode
>>> import websocket
>>> websocket.enableTrace(True)
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://echo.websocket.events")
>>> ws.send("Hello, Server", websocket.ABNF.OPCODE_TEXT) # doctest: +SKIP
>>> print(ws.recv()) # doctest: +SKIP
>>> ws.send("This is a ping", websocket.ABNF.OPCODE_PING) # doctest: +SKIP
>>> ws.close()
**WebSocketApp custom opcode code example**
The WebSocketApp class contains different functions to handle different message opcodes.
For instance, on_close, on_ping, on_pong, on_cont_message. One drawback of the current
implementation (as of May 2021) is the lack of binary support for WebSocketApp, as noted
by `issue #351 <https://github.com/websocket-client/websocket-client/issues/351>`_.
.. doctest:: custom-opcode
>>> import websocket
>>> websocket.enableTrace(True)
>>> def on_open(wsapp):
... wsapp.send("Hello")
>>> def on_message(ws, message):
... print(message)
... ws.send("Send a ping", websocket.ABNF.OPCODE_PING)
>>> def on_pong(wsapp, message):
... print("Got a pong! No need to respond")
>>> wsapp = websocket.WebSocketApp("ws://echo.websocket.events", on_open=on_open, on_message=on_message, on_pong=on_pong)
>>> wsapp.run_forever() # doctest: +SKIP
Dispatching Multiple WebSocketApps
==================================
You can use an asynchronous dispatcher such as `rel <https://pypi.org/project/rel/>`_ to run multiple WebSocketApps in the same application without resorting to threads.
**WebSocketApp asynchronous dispatcher code example**
.. doctest:: multiple-dispatchers
>>> import websocket, rel
>>> addr = "wss://api.gemini.com/v1/marketdata/%s"
>>> for symbol in ["BTCUSD", "ETHUSD", "ETHBTC"]:
... ws = websocket.WebSocketApp(addr % (symbol,), on_message=lambda w, m : print(m))
... ws.run_forever(dispatcher=rel, reconnect=3) # doctest: +SKIP
>>> rel.signal(2, rel.abort) # Keyboard Interrupt # doctest: +SKIP
>>> rel.dispatch() # doctest: +SKIP
README Examples
=========================
The examples are found in the README and are copied here for sphinx doctests to verify they run without errors.
**Long-lived Connection**
.. doctest:: long-lived-connection
>>> import websocket
>>> import _thread
>>> import time
>>> import rel
>>> def on_message(ws, message):
... print(message)
>>> def on_error(ws, error):
... print(error)
>>> def on_close(ws, close_status_code, close_msg):
... print("### closed ###")
>>> def on_open(ws):
... print("Opened connection")
>>> if __name__ == "__main__":
... websocket.enableTrace(True)
... ws = websocket.WebSocketApp("wss://api.gemini.com/v1/marketdata/BTCUSD",
... on_open=on_open,
... on_message=on_message,
... on_error=on_error,
... on_close=on_close)
>>> ws.run_forever(dispatcher=rel, reconnect=5) # Set dispatcher to automatic reconnection, 5 second reconnect delay if connection closed unexpectedly # doctest: +SKIP
>>> rel.signal(2, rel.abort) # Keyboard Interrupt
<Signal Object | Callback:"abort">
>>> rel.dispatch() # doctest: +SKIP
**Short-lived Connection**
.. doctest:: short-lived-connection
>>> from websocket import create_connection
>>> ws = create_connection("ws://echo.websocket.events/")
>>> print(ws.recv())
echo.websocket.events sponsored by Lob.com
>>> print("Sending 'Hello, World'...")
Sending 'Hello, World'...
>>> ws.send("Hello, World")
18
>>> print("Sent")
Sent
>>> print("Receiving...")
Receiving...
>>> result = ws.recv()
>>> print("Received '%s'" % result)
Received ...
>>> ws.close()
Real-world Examples
=========================
Other projects that depend on websocket-client may be able to illustrate more
complex use cases for this library. A list of 600+ dependent projects is found
`on libraries.io <https://libraries.io/pypi/websocket-client/dependents>`_, and
a few of the projects using websocket-client are listed below:
- `https://github.com/apache/airflow <https://github.com/apache/airflow>`_
- `https://github.com/docker/docker-py <https://github.com/docker/docker-py>`_
- `https://github.com/scrapinghub/slackbot <https://github.com/scrapinghub/slackbot>`_
- `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/llimllib/limbo <https://github.com/llimllib/limbo>`_
- `https://github.com/miguelgrinberg/python-socketio <https://github.com/miguelgrinberg/python-socketio>`_
- `https://github.com/invisibleroads/socketIO-client <https://github.com/invisibleroads/socketIO-client>`_
- `https://github.com/s4w3d0ff/python-poloniex <https://github.com/s4w3d0ff/python-poloniex>`_
- `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>`_
|