Debian Codegraph @

docs/faq/client.rst

raw
1 Client
2 ======
3
4 .. currentmodule:: websockets.asyncio.client
5
6 .. admonition:: This FAQ is written for the new :mod:`asyncio` implementation.
7 :class: tip
8
9 Answers are also valid for the legacy :mod:`asyncio` implementation.
10
11 They translate to the :mod:`threading` implementation by removing ``await``
12 and ``async`` keywords and by using a :class:`~threading.Thread` instead of
13 a :class:`~asyncio.Task` for concurrent execution.
14
15 Why does the client close the connection prematurely?
16 -----------------------------------------------------
17
18 You're exiting the context manager prematurely. Wait for the work to be
19 finished before exiting.
20
21 For example, if your code has a structure similar to::
22
23 async with connect(...) as websocket:
24 asyncio.create_task(do_some_work())
25
26 change it to::
27
28 async with connect(...) as websocket:
29 await do_some_work()
30
31 How do I access HTTP headers?
32 -----------------------------
33
34 Once the connection is established, HTTP headers are available in the
35 :attr:`~ClientConnection.request` and :attr:`~ClientConnection.response`
36 objects::
37
38 async with connect(...) as websocket:
39 websocket.request.headers
40 websocket.response.headers
41
42 How do I set HTTP headers?
43 --------------------------
44
45 To set the ``Origin``, ``Sec-WebSocket-Extensions``, or
46 ``Sec-WebSocket-Protocol`` headers in the WebSocket handshake request, use the
47 ``origin``, ``extensions``, or ``subprotocols`` arguments of :func:`~connect`.
48
49 To override the ``User-Agent`` header, use the ``user_agent_header`` argument.
50 Set it to :obj:`None` to remove the header.
51
52 To set other HTTP headers, for example the ``Authorization`` header, use the
53 ``additional_headers`` argument::
54
55 async with connect(..., additional_headers={"Authorization": ...}) as websocket:
56 ...
57
58 In the legacy :mod:`asyncio` API, this argument is named ``extra_headers``.
59
60 How do I force the IP address that the client connects to?
61 ----------------------------------------------------------
62
63 Use the ``host`` argument of :func:`~connect`::
64
65 async with connect(..., host="192.168.0.1") as websocket:
66 ...
67
68 :func:`~connect` accepts the same arguments as
69 :meth:`~asyncio.loop.create_connection` and passes them through.
70
71 How do I close a connection?
72 ----------------------------
73
74 The easiest is to use :func:`~connect` as a context manager::
75
76 async with connect(...) as websocket:
77 ...
78
79 The connection is closed when exiting the context manager.
80
81 How do I reconnect when the connection drops?
82 ---------------------------------------------
83
84 Use :func:`connect` as an asynchronous iterator::
85
86 from websockets.asyncio.client import connect
87 from websockets.exceptions import ConnectionClosed
88
89 async for websocket in connect(...):
90 try:
91 ...
92 except ConnectionClosed:
93 continue
94
95 Make sure you handle exceptions in the ``async for`` loop. Uncaught exceptions
96 will break out of the loop.
97
98 How do I stop a client that is processing messages in a loop?
99 -------------------------------------------------------------
100
101 You can close the connection.
102
103 Here's an example that terminates cleanly when it receives SIGTERM on Unix:
104
105 .. literalinclude:: ../../example/faq/shutdown_client.py
106 :emphasize-lines: 10-14
107
108 How do I disable TLS/SSL certificate verification?
109 --------------------------------------------------
110
111 Look at the ``ssl`` argument of :meth:`~asyncio.loop.create_connection`.
112
113 :func:`~connect` accepts the same arguments as
114 :meth:`~asyncio.loop.create_connection` and passes them through.