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/httpx.rst
+78-7Lines changed: 78 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
httpx
2
2
=====
3
3
4
-
The `httpx<https://www.python-httpx.org/>`_ library is a modern HTTP client for Python with support for both sync and async requests. This page describes how to use httpx with proxies and how to interact with proxy headers.
4
+
`HTTPX<https://www.python-httpx.org/>`_ is a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2. This page describes how to use httpx with proxies and how to interact with proxy headers.
5
5
6
6
Using Proxies with httpx
7
7
------------------------
@@ -11,7 +11,7 @@ httpx provides built-in support for proxies through the ``httpx.Proxy`` class. Y
11
11
Basic Proxy Usage
12
12
~~~~~~~~~~~~~~~~~
13
13
14
-
httpx also supports proxy headers by default, though it's not documented. You can use the ``httpx.Proxy`` class with custom headers:
14
+
httpx supports sending proxy headers by default, though it's not documented. You can use the ``httpx.Proxy`` class with custom headers:
15
15
16
16
.. code-block:: python
17
17
@@ -27,7 +27,7 @@ This creates a proxy with custom headers and uses it with an httpx client.
27
27
Receiving Proxy Response Headers
28
28
---------------------------------
29
29
30
-
But to get the response headers, you need to use our extension module ``python_proxy_headers.httpx_proxy``:
30
+
But to get response headers from a proxy server, you need to use our extension module ``python_proxy_headers.httpx_proxy``:
31
31
32
32
.. code-block:: python
33
33
@@ -40,12 +40,12 @@ But to get the response headers, you need to use our extension module ``python_p
40
40
41
41
r.headers['X-ProxyMesh-IP']
42
42
43
-
The ``HTTPProxyTransport`` from our extension module extends the standard transport to make proxy response headers available in the response headers.
43
+
The ``HTTPProxyTransport`` class from our extension module extends the standard transport to make proxy response headers available in the response headers.
44
44
45
45
Helper Methods
46
46
--------------
47
47
48
-
This module also provides helper methods similar to requests for convenience:
48
+
This module also provides helper methods similar to ``requests`` for convenience:
49
49
50
50
.. code-block:: python
51
51
@@ -136,13 +136,50 @@ For simpler use cases, you can use the helper methods:
136
136
# POST request
137
137
r = httpx_proxy.post('https://api.example.com', json={'key': 'value'}, proxy=proxy)
138
138
139
+
# PUT request
140
+
r = httpx_proxy.put('https://api.example.com/resource/1', json={'name': 'updated'}, proxy=proxy)
141
+
142
+
# PATCH request
143
+
r = httpx_proxy.patch('https://api.example.com/resource/1', json={'status': 'active'}, proxy=proxy)
144
+
145
+
# DELETE request
146
+
r = httpx_proxy.delete('https://api.example.com/resource/1', proxy=proxy)
147
+
148
+
# HEAD request
149
+
r = httpx_proxy.head('https://api.example.com', proxy=proxy)
150
+
151
+
# OPTIONS request
152
+
r = httpx_proxy.options('https://api.example.com', proxy=proxy)
153
+
139
154
# Access proxy response headers
140
155
proxy_ip = r.headers.get('X-ProxyMesh-IP')
141
156
157
+
Streaming Responses
158
+
~~~~~~~~~~~~~~~~~~~~
159
+
160
+
For streaming large responses, you can use the ``stream`` context manager:
with httpx_proxy.stream('GET', 'https://api.example.com/large-file', proxy=proxy) as response:
170
+
# Access proxy response headers
171
+
proxy_ip = response.headers.get('X-ProxyMesh-IP')
172
+
print(f"Proxy IP: {proxy_ip}")
173
+
174
+
# Stream the response content
175
+
for chunk in response.iter_bytes():
176
+
# Process each chunk as it arrives
177
+
print(f"Received {len(chunk)} bytes")
178
+
142
179
Proxy Headers Overview
143
180
----------------------
144
181
145
-
Proxy headers are custom HTTP headers that can be used to communicate with proxy servers. They allow you to:
182
+
Proxy headers are custom HTTP headers that can be used to communicate with proxy servers. They can allow you to:
146
183
147
184
* **Control proxy behavior**: Send headers like ``X-ProxyMesh-Country`` to select a specific country for your proxy connection
148
185
* **Receive proxy information**: Get headers like ``X-ProxyMesh-IP`` to know which IP address was assigned to your request
@@ -153,5 +190,39 @@ The exact headers available depend on your proxy provider. Check your proxy prov
153
190
httpcore Integration
154
191
--------------------
155
192
156
-
Our httpx helper module internally provides extension classes for `httpcore <https://www.encode.io/httpcore/>`_, for handling proxy headers over tunnel connections. You can use those classes if you're building on top of httpcore directly.
193
+
Our httpx helper module internally provides extension classes for `httpcore <https://www.encode.io/httpcore/>`_, for handling proxy headers over tunnel connections. These classes extend httpcore's internal proxy implementation to capture and merge proxy response headers.
194
+
195
+
Extension Classes
196
+
~~~~~~~~~~~~~~~~~
197
+
198
+
The module provides four main extension classes:
199
+
200
+
* ``ProxyTunnelHTTPConnection`` - Extends ``httpcore._sync.http_proxy.TunnelHTTPConnection`` to merge proxy response headers from the CONNECT response into the final HTTP response
201
+
* ``AsyncProxyTunnelHTTPConnection`` - Async version that extends ``httpcore._async.http_proxy.AsyncTunnelHTTPConnection``
202
+
* ``HTTPProxyHeaders`` - Extends ``httpcore._sync.http_proxy.HTTPProxy`` to use the custom tunnel connection classes for HTTPS connections
203
+
* ``AsyncHTTPProxyHeaders`` - Async version that extends ``httpcore._async.http_proxy.AsyncHTTPProxy``
204
+
205
+
These classes are used internally by ``HTTPProxyTransport`` and ``AsyncHTTPProxyTransport``. They automatically merge proxy response headers (like ``X-ProxyMesh-IP``) from the CONNECT response into the final HTTP response headers, making them accessible to your application.
206
+
207
+
How It Works
208
+
~~~~~~~~~~~~
209
+
210
+
The extension classes work by intercepting the CONNECT request/response cycle during tunnel establishment:
211
+
212
+
1. **CONNECT Request**: When establishing a tunnel connection through the proxy, ``ProxyTunnelHTTPConnection`` sends the CONNECT request with your custom proxy headers (e.g., ``X-ProxyMesh-Country: US``)
213
+
214
+
2. **CONNECT Response**: The proxy responds with a CONNECT response (status 200) that may include proxy information headers in the response (e.g., ``X-ProxyMesh-IP: 192.168.1.1``)
215
+
216
+
3. **Header Merging**: These proxy response headers are captured during the tunnel establishment and stored. When the actual HTTP request is made through the tunnel, the proxy response headers are merged into the final HTTP response headers using ``merge_headers()``
217
+
218
+
4. **Access**: Your application can then access both the target server's response headers and the proxy's response headers from the same response object
219
+
220
+
This is particularly useful for proxy services that provide metadata about the proxy connection (such as the assigned IP address, country, or session information) in the CONNECT response headers.
221
+
222
+
Internal Usage
223
+
~~~~~~~~~~~~~~
224
+
225
+
These classes are used internally by ``HTTPProxyTransport`` and ``AsyncHTTPProxyTransport``. When you create a transport with a proxy, it automatically uses ``HTTPProxyHeaders`` (or ``AsyncHTTPProxyHeaders``) as the connection pool, which in turn uses ``ProxyTunnelHTTPConnection`` (or ``AsyncProxyTunnelHTTPConnection``) for HTTPS tunnel connections.
226
+
227
+
If you're building custom functionality on top of httpcore, you can import and use these classes directly, but note that they depend on httpcore's internal APIs which may change between versions.
0 commit comments