Debian Codegraph @

docs/topics/proxies.rst

raw
1 Proxies
2 =======
3
4 .. currentmodule:: websockets
5
6 If a proxy is configured in the operating system or with an environment
7 variable, websockets uses it automatically when connecting to a server.
8
9 Configuration
10 -------------
11
12 First, if the server is in the proxy bypass list of the operating system or in
13 the ``no_proxy`` environment variable, websockets connects directly.
14
15 Then, it looks for a proxy in the following locations:
16
17 1. The ``wss_proxy`` or ``ws_proxy`` environment variables for ``wss://`` and
18 ``ws://`` connections respectively. They allow configuring a specific proxy
19 for WebSocket connections.
20 2. A SOCKS proxy configured in the operating system.
21 3. An HTTP proxy configured in the operating system or in the ``https_proxy``
22 environment variable, for both ``wss://`` and ``ws://`` connections.
23 4. An HTTP proxy configured in the operating system or in the ``http_proxy``
24 environment variable, only for ``ws://`` connections.
25
26 Finally, if no proxy is found, websockets connects directly.
27
28 While environment variables are case-insensitive, the lower-case spelling is the
29 most common, for `historical reasons`_, and recommended.
30
31 .. _historical reasons: https://unix.stackexchange.com/questions/212894/
32
33 websockets authenticates automatically when the address of the proxy includes
34 credentials e.g. ``http://user:password@proxy:8080/``.
35
36 .. admonition:: Any environment variable can configure a SOCKS proxy or an HTTP proxy.
37 :class: tip
38
39 For example, ``https_proxy=socks5h://proxy:1080/`` configures a SOCKS proxy
40 for all WebSocket connections. Likewise, ``wss_proxy=http://proxy:8080/``
41 configures an HTTP proxy only for ``wss://`` connections.
42
43 .. admonition:: What if websockets doesn't select the right proxy?
44 :class: hint
45
46 websockets relies on :func:`~urllib.request.getproxies()` to read the proxy
47 configuration. Check that it returns what you expect. If it doesn't, review
48 your proxy configuration.
49
50 You can override the default configuration and configure a proxy explicitly with
51 the ``proxy`` argument of :func:`~asyncio.client.connect`. Set ``proxy=None`` to
52 disable the proxy.
53
54 SOCKS proxies
55 -------------
56
57 Connecting through a SOCKS proxy requires installing the third-party library
58 `python-socks`_:
59
60 .. code-block:: console
61
62 $ pip install python-socks\[asyncio\]
63
64 .. _python-socks: https://github.com/romis2012/python-socks
65
66 python-socks supports SOCKS4, SOCKS4a, SOCKS5, and SOCKS5h. The protocol version
67 is configured in the address of the proxy e.g. ``socks5h://proxy:1080/``. When a
68 SOCKS proxy is configured in the operating system, python-socks uses SOCKS5h.
69
70 python-socks supports username/password authentication for SOCKS5 (:rfc:`1929`)
71 but does not support other authentication methods such as GSSAPI (:rfc:`1961`).
72
73 HTTP proxies
74 ------------
75
76 When the address of the proxy starts with ``https://``, websockets secures the
77 connection to the proxy with TLS.
78
79 When the address of the server starts with ``wss://``, websockets secures the
80 connection from the proxy to the server with TLS.
81
82 These two options are compatible. TLS-in-TLS is supported.
83
84 The documentation of :func:`~asyncio.client.connect` describes how to configure
85 TLS from websockets to the proxy and from the proxy to the server.
86
87 websockets supports proxy authentication with Basic Auth.