You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/urllib3.rst
+49-1Lines changed: 49 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ The ``proxy_headers`` parameter allows you to send custom headers to the proxy s
36
36
37
37
.. note::
38
38
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.
40
40
41
41
Receiving Proxy Response Headers
42
42
---------------------------------
@@ -66,6 +66,21 @@ You can also pass ``proxy_headers`` into our ``ProxyHeaderManager`` as well. For
66
66
67
67
This allows you to both send custom headers to the proxy and receive proxy response headers in a single request.
68
68
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
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
+
69
84
Proxy Headers Overview
70
85
----------------------
71
86
@@ -77,3 +92,36 @@ Proxy headers are custom HTTP headers that can be used to communicate with proxy
77
92
78
93
The exact headers available depend on your proxy provider. Check your proxy provider's documentation for the specific headers they support.
79
94
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.
0 commit comments