Skip to content

Commit e70bf3d

Browse files
committed
more urllib3 docs
1 parent 346ee17 commit e70bf3d

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

docs/urllib3.rst

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The ``proxy_headers`` parameter allows you to send custom headers to the proxy s
3636

3737
.. note::
3838

39-
When using this method, if you keep reusing the same ``ProxyManager`` instance, you may be re-using the proxy connection, which may have different behavior than if you create a new proxy connection for each request. For example, with ProxyMesh you may keep getting the same IP address if you reuse the proxy connection.
39+
When using this method, if you keep reusing the same ``ProxyManager`` instance, you may be re-using the proxy connection, which may have different behavior than if you create a new proxy connection for each request. For example, with `ProxyMesh <https://proxymesh.com>`_ you may keep getting the same IP address if you reuse the proxy connection.
4040

4141
Receiving Proxy Response Headers
4242
---------------------------------
@@ -66,6 +66,21 @@ You can also pass ``proxy_headers`` into our ``ProxyHeaderManager`` as well. For
6666
6767
This allows you to both send custom headers to the proxy and receive proxy response headers in a single request.
6868

69+
Helper Function
70+
~~~~~~~~~~~~~~~
71+
72+
The module also provides a convenience function for creating a ``ProxyHeaderManager``:
73+
74+
.. code-block:: python
75+
76+
from python_proxy_headers.urllib3_proxy_manager import proxy_from_url
77+
78+
proxy = proxy_from_url('http://PROXYHOST:PORT', proxy_headers={'X-ProxyMesh-Country': 'US'})
79+
r = proxy.request('GET', 'https://api.ipify.org?format=json')
80+
r.headers['X-ProxyMesh-IP']
81+
82+
The ``proxy_from_url()`` function is a convenience wrapper around ``ProxyHeaderManager`` that creates a proxy manager from a URL string, similar to urllib3's standard ``proxy_from_url()`` function.
83+
6984
Proxy Headers Overview
7085
----------------------
7186

@@ -77,3 +92,36 @@ Proxy headers are custom HTTP headers that can be used to communicate with proxy
7792

7893
The exact headers available depend on your proxy provider. Check your proxy provider's documentation for the specific headers they support.
7994

95+
Internal Classes
96+
----------------
97+
98+
The ``ProxyHeaderManager`` internally uses extension classes that extend urllib3's connection and connection pool classes:
99+
100+
* ``HTTPSProxyConnection`` - Extends ``urllib3.connection.HTTPSConnection`` to capture proxy response headers from the CONNECT response
101+
* ``HTTPSProxyConnectionPool`` - Extends ``urllib3.connectionpool.HTTPSConnectionPool`` to merge proxy response headers into the final HTTP response
102+
103+
These classes work together to make proxy response headers available in your application's response object.
104+
105+
HTTPSProxyConnection
106+
~~~~~~~~~~~~~~~~~~~~
107+
108+
The ``HTTPSProxyConnection`` class extends the standard ``HTTPSConnection`` to capture proxy response headers during tunnel establishment. When a CONNECT request is made to establish the tunnel, it:
109+
110+
1. Sends the CONNECT request with any custom proxy headers
111+
2. Reads the CONNECT response from the proxy server
112+
3. Captures the proxy response headers (e.g., ``X-ProxyMesh-IP``)
113+
4. Stores them for later retrieval via ``get_proxy_response_headers()``
114+
115+
The ``get_proxy_response_headers()`` method returns a dictionary containing the headers from the proxy's CONNECT response, or ``None`` if the CONNECT request hasn't been sent yet.
116+
117+
HTTPSProxyConnectionPool
118+
~~~~~~~~~~~~~~~~~~~~~~~~
119+
120+
The ``HTTPSProxyConnectionPool`` class extends ``HTTPSConnectionPool`` to automatically merge proxy response headers into the final HTTP response. It:
121+
122+
1. Uses ``HTTPSProxyConnection`` as its connection class
123+
2. Captures proxy response headers after preparing the proxy connection
124+
3. Merges these headers into the response headers when ``urlopen()`` is called
125+
126+
This ensures that proxy response headers are automatically available in the response object returned to your application.
127+

0 commit comments

Comments
 (0)