poolmanager.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. from __future__ import annotations
  2. import functools
  3. import logging
  4. import typing
  5. import warnings
  6. from types import TracebackType
  7. from urllib.parse import urljoin
  8. from ._collections import HTTPHeaderDict, RecentlyUsedContainer
  9. from ._request_methods import RequestMethods
  10. from .connection import ProxyConfig
  11. from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, port_by_scheme
  12. from .exceptions import (
  13. LocationValueError,
  14. MaxRetryError,
  15. ProxySchemeUnknown,
  16. URLSchemeUnknown,
  17. )
  18. from .response import BaseHTTPResponse
  19. from .util.connection import _TYPE_SOCKET_OPTIONS
  20. from .util.proxy import connection_requires_http_tunnel
  21. from .util.retry import Retry
  22. from .util.timeout import Timeout
  23. from .util.url import Url, parse_url
  24. if typing.TYPE_CHECKING:
  25. import ssl
  26. from typing_extensions import Self
  27. __all__ = ["PoolManager", "ProxyManager", "proxy_from_url"]
  28. log = logging.getLogger(__name__)
  29. SSL_KEYWORDS = (
  30. "key_file",
  31. "cert_file",
  32. "cert_reqs",
  33. "ca_certs",
  34. "ca_cert_data",
  35. "ssl_version",
  36. "ssl_minimum_version",
  37. "ssl_maximum_version",
  38. "ca_cert_dir",
  39. "ssl_context",
  40. "key_password",
  41. "server_hostname",
  42. )
  43. # Default value for `blocksize` - a new parameter introduced to
  44. # http.client.HTTPConnection & http.client.HTTPSConnection in Python 3.7
  45. _DEFAULT_BLOCKSIZE = 16384
  46. class PoolKey(typing.NamedTuple):
  47. """
  48. All known keyword arguments that could be provided to the pool manager, its
  49. pools, or the underlying connections.
  50. All custom key schemes should include the fields in this key at a minimum.
  51. """
  52. key_scheme: str
  53. key_host: str
  54. key_port: int | None
  55. key_timeout: Timeout | float | int | None
  56. key_retries: Retry | bool | int | None
  57. key_block: bool | None
  58. key_source_address: tuple[str, int] | None
  59. key_key_file: str | None
  60. key_key_password: str | None
  61. key_cert_file: str | None
  62. key_cert_reqs: str | None
  63. key_ca_certs: str | None
  64. key_ca_cert_data: str | bytes | None
  65. key_ssl_version: int | str | None
  66. key_ssl_minimum_version: ssl.TLSVersion | None
  67. key_ssl_maximum_version: ssl.TLSVersion | None
  68. key_ca_cert_dir: str | None
  69. key_ssl_context: ssl.SSLContext | None
  70. key_maxsize: int | None
  71. key_headers: frozenset[tuple[str, str]] | None
  72. key__proxy: Url | None
  73. key__proxy_headers: frozenset[tuple[str, str]] | None
  74. key__proxy_config: ProxyConfig | None
  75. key_socket_options: _TYPE_SOCKET_OPTIONS | None
  76. key__socks_options: frozenset[tuple[str, str]] | None
  77. key_assert_hostname: bool | str | None
  78. key_assert_fingerprint: str | None
  79. key_server_hostname: str | None
  80. key_blocksize: int | None
  81. def _default_key_normalizer(
  82. key_class: type[PoolKey], request_context: dict[str, typing.Any]
  83. ) -> PoolKey:
  84. """
  85. Create a pool key out of a request context dictionary.
  86. According to RFC 3986, both the scheme and host are case-insensitive.
  87. Therefore, this function normalizes both before constructing the pool
  88. key for an HTTPS request. If you wish to change this behaviour, provide
  89. alternate callables to ``key_fn_by_scheme``.
  90. :param key_class:
  91. The class to use when constructing the key. This should be a namedtuple
  92. with the ``scheme`` and ``host`` keys at a minimum.
  93. :type key_class: namedtuple
  94. :param request_context:
  95. A dictionary-like object that contain the context for a request.
  96. :type request_context: dict
  97. :return: A namedtuple that can be used as a connection pool key.
  98. :rtype: PoolKey
  99. """
  100. # Since we mutate the dictionary, make a copy first
  101. context = request_context.copy()
  102. context["scheme"] = context["scheme"].lower()
  103. context["host"] = context["host"].lower()
  104. # These are both dictionaries and need to be transformed into frozensets
  105. for key in ("headers", "_proxy_headers", "_socks_options"):
  106. if key in context and context[key] is not None:
  107. context[key] = frozenset(context[key].items())
  108. # The socket_options key may be a list and needs to be transformed into a
  109. # tuple.
  110. socket_opts = context.get("socket_options")
  111. if socket_opts is not None:
  112. context["socket_options"] = tuple(socket_opts)
  113. # Map the kwargs to the names in the namedtuple - this is necessary since
  114. # namedtuples can't have fields starting with '_'.
  115. for key in list(context.keys()):
  116. context["key_" + key] = context.pop(key)
  117. # Default to ``None`` for keys missing from the context
  118. for field in key_class._fields:
  119. if field not in context:
  120. context[field] = None
  121. # Default key_blocksize to _DEFAULT_BLOCKSIZE if missing from the context
  122. if context.get("key_blocksize") is None:
  123. context["key_blocksize"] = _DEFAULT_BLOCKSIZE
  124. return key_class(**context)
  125. #: A dictionary that maps a scheme to a callable that creates a pool key.
  126. #: This can be used to alter the way pool keys are constructed, if desired.
  127. #: Each PoolManager makes a copy of this dictionary so they can be configured
  128. #: globally here, or individually on the instance.
  129. key_fn_by_scheme = {
  130. "http": functools.partial(_default_key_normalizer, PoolKey),
  131. "https": functools.partial(_default_key_normalizer, PoolKey),
  132. }
  133. pool_classes_by_scheme = {"http": HTTPConnectionPool, "https": HTTPSConnectionPool}
  134. class PoolManager(RequestMethods):
  135. """
  136. Allows for arbitrary requests while transparently keeping track of
  137. necessary connection pools for you.
  138. :param num_pools:
  139. Number of connection pools to cache before discarding the least
  140. recently used pool.
  141. :param headers:
  142. Headers to include with all requests, unless other headers are given
  143. explicitly.
  144. :param \\**connection_pool_kw:
  145. Additional parameters are used to create fresh
  146. :class:`urllib3.connectionpool.ConnectionPool` instances.
  147. Example:
  148. .. code-block:: python
  149. import urllib3
  150. http = urllib3.PoolManager(num_pools=2)
  151. resp1 = http.request("GET", "https://google.com/")
  152. resp2 = http.request("GET", "https://google.com/mail")
  153. resp3 = http.request("GET", "https://yahoo.com/")
  154. print(len(http.pools))
  155. # 2
  156. """
  157. proxy: Url | None = None
  158. proxy_config: ProxyConfig | None = None
  159. def __init__(
  160. self,
  161. num_pools: int = 10,
  162. headers: typing.Mapping[str, str] | None = None,
  163. **connection_pool_kw: typing.Any,
  164. ) -> None:
  165. super().__init__(headers)
  166. # PoolManager handles redirects itself in PoolManager.urlopen().
  167. # It always passes redirect=False to the underlying connection pool to
  168. # suppress per-pool redirect handling. If the user supplied a non-Retry
  169. # value (int/bool/etc) for retries and we let the pool normalize it
  170. # while redirect=False, the resulting Retry object would have redirect
  171. # handling disabled, which can interfere with PoolManager's own
  172. # redirect logic. Normalize here so redirects remain governed solely by
  173. # PoolManager logic.
  174. if "retries" in connection_pool_kw:
  175. retries = connection_pool_kw["retries"]
  176. if not isinstance(retries, Retry):
  177. retries = Retry.from_int(retries)
  178. connection_pool_kw = connection_pool_kw.copy()
  179. connection_pool_kw["retries"] = retries
  180. self.connection_pool_kw = connection_pool_kw
  181. self.pools: RecentlyUsedContainer[PoolKey, HTTPConnectionPool]
  182. self.pools = RecentlyUsedContainer(num_pools)
  183. # Locally set the pool classes and keys so other PoolManagers can
  184. # override them.
  185. self.pool_classes_by_scheme = pool_classes_by_scheme
  186. self.key_fn_by_scheme = key_fn_by_scheme.copy()
  187. def __enter__(self) -> Self:
  188. return self
  189. def __exit__(
  190. self,
  191. exc_type: type[BaseException] | None,
  192. exc_val: BaseException | None,
  193. exc_tb: TracebackType | None,
  194. ) -> typing.Literal[False]:
  195. self.clear()
  196. # Return False to re-raise any potential exceptions
  197. return False
  198. def _new_pool(
  199. self,
  200. scheme: str,
  201. host: str,
  202. port: int,
  203. request_context: dict[str, typing.Any] | None = None,
  204. ) -> HTTPConnectionPool:
  205. """
  206. Create a new :class:`urllib3.connectionpool.ConnectionPool` based on host, port, scheme, and
  207. any additional pool keyword arguments.
  208. If ``request_context`` is provided, it is provided as keyword arguments
  209. to the pool class used. This method is used to actually create the
  210. connection pools handed out by :meth:`connection_from_url` and
  211. companion methods. It is intended to be overridden for customization.
  212. """
  213. pool_cls: type[HTTPConnectionPool] = self.pool_classes_by_scheme[scheme]
  214. if request_context is None:
  215. request_context = self.connection_pool_kw.copy()
  216. # Default blocksize to _DEFAULT_BLOCKSIZE if missing or explicitly
  217. # set to 'None' in the request_context.
  218. if request_context.get("blocksize") is None:
  219. request_context["blocksize"] = _DEFAULT_BLOCKSIZE
  220. # Although the context has everything necessary to create the pool,
  221. # this function has historically only used the scheme, host, and port
  222. # in the positional args. When an API change is acceptable these can
  223. # be removed.
  224. for key in ("scheme", "host", "port"):
  225. request_context.pop(key, None)
  226. if scheme == "http":
  227. for kw in SSL_KEYWORDS:
  228. request_context.pop(kw, None)
  229. return pool_cls(host, port, **request_context)
  230. def clear(self) -> None:
  231. """
  232. Empty our store of pools and direct them all to close.
  233. This will not affect in-flight connections, but they will not be
  234. re-used after completion.
  235. """
  236. self.pools.clear()
  237. def connection_from_host(
  238. self,
  239. host: str | None,
  240. port: int | None = None,
  241. scheme: str | None = "http",
  242. pool_kwargs: dict[str, typing.Any] | None = None,
  243. ) -> HTTPConnectionPool:
  244. """
  245. Get a :class:`urllib3.connectionpool.ConnectionPool` based on the host, port, and scheme.
  246. If ``port`` isn't given, it will be derived from the ``scheme`` using
  247. ``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is
  248. provided, it is merged with the instance's ``connection_pool_kw``
  249. variable and used to create the new connection pool, if one is
  250. needed.
  251. """
  252. if not host:
  253. raise LocationValueError("No host specified.")
  254. request_context = self._merge_pool_kwargs(pool_kwargs)
  255. request_context["scheme"] = scheme or "http"
  256. if not port:
  257. port = port_by_scheme.get(request_context["scheme"].lower(), 80)
  258. request_context["port"] = port
  259. request_context["host"] = host
  260. return self.connection_from_context(request_context)
  261. def connection_from_context(
  262. self, request_context: dict[str, typing.Any]
  263. ) -> HTTPConnectionPool:
  264. """
  265. Get a :class:`urllib3.connectionpool.ConnectionPool` based on the request context.
  266. ``request_context`` must at least contain the ``scheme`` key and its
  267. value must be a key in ``key_fn_by_scheme`` instance variable.
  268. """
  269. if "strict" in request_context:
  270. warnings.warn(
  271. "The 'strict' parameter is no longer needed on Python 3+. "
  272. "This will raise an error in urllib3 v2.1.0.",
  273. DeprecationWarning,
  274. )
  275. request_context.pop("strict")
  276. scheme = request_context["scheme"].lower()
  277. pool_key_constructor = self.key_fn_by_scheme.get(scheme)
  278. if not pool_key_constructor:
  279. raise URLSchemeUnknown(scheme)
  280. pool_key = pool_key_constructor(request_context)
  281. return self.connection_from_pool_key(pool_key, request_context=request_context)
  282. def connection_from_pool_key(
  283. self, pool_key: PoolKey, request_context: dict[str, typing.Any]
  284. ) -> HTTPConnectionPool:
  285. """
  286. Get a :class:`urllib3.connectionpool.ConnectionPool` based on the provided pool key.
  287. ``pool_key`` should be a namedtuple that only contains immutable
  288. objects. At a minimum it must have the ``scheme``, ``host``, and
  289. ``port`` fields.
  290. """
  291. with self.pools.lock:
  292. # If the scheme, host, or port doesn't match existing open
  293. # connections, open a new ConnectionPool.
  294. pool = self.pools.get(pool_key)
  295. if pool:
  296. return pool
  297. # Make a fresh ConnectionPool of the desired type
  298. scheme = request_context["scheme"]
  299. host = request_context["host"]
  300. port = request_context["port"]
  301. pool = self._new_pool(scheme, host, port, request_context=request_context)
  302. self.pools[pool_key] = pool
  303. return pool
  304. def connection_from_url(
  305. self, url: str, pool_kwargs: dict[str, typing.Any] | None = None
  306. ) -> HTTPConnectionPool:
  307. """
  308. Similar to :func:`urllib3.connectionpool.connection_from_url`.
  309. If ``pool_kwargs`` is not provided and a new pool needs to be
  310. constructed, ``self.connection_pool_kw`` is used to initialize
  311. the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``
  312. is provided, it is used instead. Note that if a new pool does not
  313. need to be created for the request, the provided ``pool_kwargs`` are
  314. not used.
  315. """
  316. u = parse_url(url)
  317. return self.connection_from_host(
  318. u.host, port=u.port, scheme=u.scheme, pool_kwargs=pool_kwargs
  319. )
  320. def _merge_pool_kwargs(
  321. self, override: dict[str, typing.Any] | None
  322. ) -> dict[str, typing.Any]:
  323. """
  324. Merge a dictionary of override values for self.connection_pool_kw.
  325. This does not modify self.connection_pool_kw and returns a new dict.
  326. Any keys in the override dictionary with a value of ``None`` are
  327. removed from the merged dictionary.
  328. """
  329. base_pool_kwargs = self.connection_pool_kw.copy()
  330. if override:
  331. for key, value in override.items():
  332. if value is None:
  333. try:
  334. del base_pool_kwargs[key]
  335. except KeyError:
  336. pass
  337. else:
  338. base_pool_kwargs[key] = value
  339. return base_pool_kwargs
  340. def _proxy_requires_url_absolute_form(self, parsed_url: Url) -> bool:
  341. """
  342. Indicates if the proxy requires the complete destination URL in the
  343. request. Normally this is only needed when not using an HTTP CONNECT
  344. tunnel.
  345. """
  346. if self.proxy is None:
  347. return False
  348. return not connection_requires_http_tunnel(
  349. self.proxy, self.proxy_config, parsed_url.scheme
  350. )
  351. def urlopen( # type: ignore[override]
  352. self, method: str, url: str, redirect: bool = True, **kw: typing.Any
  353. ) -> BaseHTTPResponse:
  354. """
  355. Same as :meth:`urllib3.HTTPConnectionPool.urlopen`
  356. with custom cross-host redirect logic and only sends the request-uri
  357. portion of the ``url``.
  358. The given ``url`` parameter must be absolute, such that an appropriate
  359. :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
  360. """
  361. u = parse_url(url)
  362. if u.scheme is None:
  363. warnings.warn(
  364. "URLs without a scheme (ie 'https://') are deprecated and will raise an error "
  365. "in a future version of urllib3. To avoid this DeprecationWarning ensure all URLs "
  366. "start with 'https://' or 'http://'. Read more in this issue: "
  367. "https://github.com/urllib3/urllib3/issues/2920",
  368. category=DeprecationWarning,
  369. stacklevel=2,
  370. )
  371. conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
  372. kw["assert_same_host"] = False
  373. kw["redirect"] = False
  374. if "headers" not in kw:
  375. kw["headers"] = self.headers
  376. if self._proxy_requires_url_absolute_form(u):
  377. response = conn.urlopen(method, url, **kw)
  378. else:
  379. response = conn.urlopen(method, u.request_uri, **kw)
  380. redirect_location = redirect and response.get_redirect_location()
  381. if not redirect_location:
  382. return response
  383. # Support relative URLs for redirecting.
  384. redirect_location = urljoin(url, redirect_location)
  385. if response.status == 303:
  386. # Change the method according to RFC 9110, Section 15.4.4.
  387. method = "GET"
  388. # And lose the body not to transfer anything sensitive.
  389. kw["body"] = None
  390. kw["headers"] = HTTPHeaderDict(kw["headers"])._prepare_for_method_change()
  391. retries = kw.get("retries", response.retries)
  392. if not isinstance(retries, Retry):
  393. retries = Retry.from_int(retries, redirect=redirect)
  394. # Strip headers marked as unsafe to forward to the redirected location.
  395. # Check remove_headers_on_redirect to avoid a potential network call within
  396. # conn.is_same_host() which may use socket.gethostbyname() in the future.
  397. if retries.remove_headers_on_redirect and not conn.is_same_host(
  398. redirect_location
  399. ):
  400. new_headers = kw["headers"].copy()
  401. for header in kw["headers"]:
  402. if header.lower() in retries.remove_headers_on_redirect:
  403. new_headers.pop(header, None)
  404. kw["headers"] = new_headers
  405. try:
  406. retries = retries.increment(method, url, response=response, _pool=conn)
  407. except MaxRetryError:
  408. if retries.raise_on_redirect:
  409. response.drain_conn()
  410. raise
  411. return response
  412. kw["retries"] = retries
  413. kw["redirect"] = redirect
  414. log.info("Redirecting %s -> %s", url, redirect_location)
  415. response.drain_conn()
  416. return self.urlopen(method, redirect_location, **kw)
  417. class ProxyManager(PoolManager):
  418. """
  419. Behaves just like :class:`PoolManager`, but sends all requests through
  420. the defined proxy, using the CONNECT method for HTTPS URLs.
  421. :param proxy_url:
  422. The URL of the proxy to be used.
  423. :param proxy_headers:
  424. A dictionary containing headers that will be sent to the proxy. In case
  425. of HTTP they are being sent with each request, while in the
  426. HTTPS/CONNECT case they are sent only once. Could be used for proxy
  427. authentication.
  428. :param proxy_ssl_context:
  429. The proxy SSL context is used to establish the TLS connection to the
  430. proxy when using HTTPS proxies.
  431. :param use_forwarding_for_https:
  432. (Defaults to False) If set to True will forward requests to the HTTPS
  433. proxy to be made on behalf of the client instead of creating a TLS
  434. tunnel via the CONNECT method. **Enabling this flag means that request
  435. and response headers and content will be visible from the HTTPS proxy**
  436. whereas tunneling keeps request and response headers and content
  437. private. IP address, target hostname, SNI, and port are always visible
  438. to an HTTPS proxy even when this flag is disabled.
  439. :param proxy_assert_hostname:
  440. The hostname of the certificate to verify against.
  441. :param proxy_assert_fingerprint:
  442. The fingerprint of the certificate to verify against.
  443. Example:
  444. .. code-block:: python
  445. import urllib3
  446. proxy = urllib3.ProxyManager("https://localhost:3128/")
  447. resp1 = proxy.request("GET", "https://google.com/")
  448. resp2 = proxy.request("GET", "https://httpbin.org/")
  449. print(len(proxy.pools))
  450. # 1
  451. resp3 = proxy.request("GET", "https://httpbin.org/")
  452. resp4 = proxy.request("GET", "https://twitter.com/")
  453. print(len(proxy.pools))
  454. # 3
  455. """
  456. def __init__(
  457. self,
  458. proxy_url: str,
  459. num_pools: int = 10,
  460. headers: typing.Mapping[str, str] | None = None,
  461. proxy_headers: typing.Mapping[str, str] | None = None,
  462. proxy_ssl_context: ssl.SSLContext | None = None,
  463. use_forwarding_for_https: bool = False,
  464. proxy_assert_hostname: None | str | typing.Literal[False] = None,
  465. proxy_assert_fingerprint: str | None = None,
  466. **connection_pool_kw: typing.Any,
  467. ) -> None:
  468. if isinstance(proxy_url, HTTPConnectionPool):
  469. str_proxy_url = f"{proxy_url.scheme}://{proxy_url.host}:{proxy_url.port}"
  470. else:
  471. str_proxy_url = proxy_url
  472. proxy = parse_url(str_proxy_url)
  473. if proxy.scheme not in ("http", "https"):
  474. raise ProxySchemeUnknown(proxy.scheme)
  475. if not proxy.port:
  476. port = port_by_scheme.get(proxy.scheme, 80)
  477. proxy = proxy._replace(port=port)
  478. self.proxy = proxy
  479. self.proxy_headers = proxy_headers or {}
  480. self.proxy_ssl_context = proxy_ssl_context
  481. self.proxy_config = ProxyConfig(
  482. proxy_ssl_context,
  483. use_forwarding_for_https,
  484. proxy_assert_hostname,
  485. proxy_assert_fingerprint,
  486. )
  487. connection_pool_kw["_proxy"] = self.proxy
  488. connection_pool_kw["_proxy_headers"] = self.proxy_headers
  489. connection_pool_kw["_proxy_config"] = self.proxy_config
  490. super().__init__(num_pools, headers, **connection_pool_kw)
  491. def connection_from_host(
  492. self,
  493. host: str | None,
  494. port: int | None = None,
  495. scheme: str | None = "http",
  496. pool_kwargs: dict[str, typing.Any] | None = None,
  497. ) -> HTTPConnectionPool:
  498. if scheme == "https":
  499. return super().connection_from_host(
  500. host, port, scheme, pool_kwargs=pool_kwargs
  501. )
  502. return super().connection_from_host(
  503. self.proxy.host, self.proxy.port, self.proxy.scheme, pool_kwargs=pool_kwargs # type: ignore[union-attr]
  504. )
  505. def _set_proxy_headers(
  506. self, url: str, headers: typing.Mapping[str, str] | None = None
  507. ) -> typing.Mapping[str, str]:
  508. """
  509. Sets headers needed by proxies: specifically, the Accept and Host
  510. headers. Only sets headers not provided by the user.
  511. """
  512. headers_ = {"Accept": "*/*"}
  513. netloc = parse_url(url).netloc
  514. if netloc:
  515. headers_["Host"] = netloc
  516. if headers:
  517. headers_.update(headers)
  518. return headers_
  519. def urlopen( # type: ignore[override]
  520. self, method: str, url: str, redirect: bool = True, **kw: typing.Any
  521. ) -> BaseHTTPResponse:
  522. "Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
  523. u = parse_url(url)
  524. if not connection_requires_http_tunnel(self.proxy, self.proxy_config, u.scheme):
  525. # For connections using HTTP CONNECT, httplib sets the necessary
  526. # headers on the CONNECT to the proxy. If we're not using CONNECT,
  527. # we'll definitely need to set 'Host' at the very least.
  528. headers = kw.get("headers", self.headers)
  529. kw["headers"] = self._set_proxy_headers(url, headers)
  530. return super().urlopen(method, url, redirect=redirect, **kw)
  531. def proxy_from_url(url: str, **kw: typing.Any) -> ProxyManager:
  532. return ProxyManager(proxy_url=url, **kw)