3This module contains provisional support for SOCKS proxies from within
4urllib3. This module supports SOCKS4, SOCKS4A (an extension of SOCKS4), and
5SOCKS5. To enable its functionality, either install PySocks or install this
6module with the ``socks`` extra.
8The SOCKS implementation supports the full range of urllib3 features. It also
9supports the following SOCKS features:
11- SOCKS4A (``proxy_url='socks4a://...``)
12- SOCKS4 (``proxy_url='socks4://...``)
13- SOCKS5 with remote DNS (``proxy_url='socks5h://...``)
14- SOCKS5 with local DNS (``proxy_url='socks5://...``)
15- Usernames and passwords for the SOCKS proxy
18 It is recommended to use ``socks5h://`` or ``socks4a://`` schemes in
19 your ``proxy_url`` to ensure that DNS resolution is done from the remote
20 server instead of client-side when connecting to a domain name.
22SOCKS4 supports IPv4 and domain names with the SOCKS4A extension. SOCKS5
23supports IPv4, IPv6, and domain names.
25When connecting to a SOCKS4 proxy the ``username`` portion of the ``proxy_url``
26will be sent as the ``userid`` section of the SOCKS request:
30 proxy_url="socks4a://<userid>@proxy-host"
32When connecting to a SOCKS5 proxy the ``username`` and ``password`` portion
33of the ``proxy_url`` will be sent as the username/password to authenticate
38 proxy_url="socks5h://<username>:<password>@proxy-host"
41from __future__
import absolute_import
48 from ..exceptions
import DependencyWarning
52 "SOCKS support in urllib3 requires the installation of optional "
53 "dependencies: specifically, PySocks. For more information, see "
54 "https://urllib3.readthedocs.io/en/1.26.x/contrib.html#socks-proxies"
60from socket
import error
as SocketError
61from socket
import timeout
as SocketTimeout
63from ..connection
import HTTPConnection, HTTPSConnection
64from ..connectionpool
import HTTPConnectionPool, HTTPSConnectionPool
65from ..exceptions
import ConnectTimeoutError, NewConnectionError
66from ..poolmanager
import PoolManager
77 A plain-text HTTP connection that connects via a SOCKS proxy.
86 Establish a new connection via the SOCKS proxy.
108 except SocketTimeout:
111 "Connection to %s timed out. (connect timeout=%s)"
123 "Connection to %s timed out. (connect timeout=%s)"
128 self,
"Failed to establish a new connection: %s" % error
132 self,
"Failed to establish a new connection: %s" % e
135 except SocketError
as e:
137 self,
"Failed to establish a new connection: %s" % e
151class SOCKSHTTPConnectionPool(HTTPConnectionPool):
152 ConnectionCls = SOCKSConnection
156 ConnectionCls = SOCKSHTTPSConnection
161 A version of the urllib3 ProxyManager that routes connections via the
165 pool_classes_by_scheme = {
166 "http": SOCKSHTTPConnectionPool,
167 "https": SOCKSHTTPSConnectionPool,
179 parsed = parse_url(proxy_url)
181 if username
is None and password
is None and parsed.auth is not None:
184 username, password = split
198 raise ValueError(
"Unable to determine SOCKS version from %s" % proxy_url)
203 "socks_version": socks_version,
206 "username": username,
207 "password": password,
210 connection_pool_kw[
"_socks_options"] = socks_options
213 num_pools, headers, **connection_pool_kw
__init__(self, *args, **kwargs)
__init__(self, proxy_url, username=None, password=None, num_pools=10, headers=None, **connection_pool_kw)
dict pool_classes_by_scheme