Skip to content

Commit 346ee17

Browse files
committed
more httpx documentation
1 parent 4c8034a commit 346ee17

1 file changed

Lines changed: 78 additions & 7 deletions

File tree

docs/httpx.rst

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
httpx
22
=====
33

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.
55

66
Using Proxies with httpx
77
------------------------
@@ -11,7 +11,7 @@ httpx provides built-in support for proxies through the ``httpx.Proxy`` class. Y
1111
Basic Proxy Usage
1212
~~~~~~~~~~~~~~~~~
1313

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:
1515

1616
.. code-block:: python
1717
@@ -27,7 +27,7 @@ This creates a proxy with custom headers and uses it with an httpx client.
2727
Receiving Proxy Response Headers
2828
---------------------------------
2929

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``:
3131

3232
.. code-block:: python
3333
@@ -40,12 +40,12 @@ But to get the response headers, you need to use our extension module ``python_p
4040
4141
r.headers['X-ProxyMesh-IP']
4242
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.
4444

4545
Helper Methods
4646
--------------
4747

48-
This module also provides helper methods similar to requests for convenience:
48+
This module also provides helper methods similar to ``requests`` for convenience:
4949

5050
.. code-block:: python
5151
@@ -136,13 +136,50 @@ For simpler use cases, you can use the helper methods:
136136
# POST request
137137
r = httpx_proxy.post('https://api.example.com', json={'key': 'value'}, proxy=proxy)
138138
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+
139154
# Access proxy response headers
140155
proxy_ip = r.headers.get('X-ProxyMesh-IP')
141156
157+
Streaming Responses
158+
~~~~~~~~~~~~~~~~~~~~
159+
160+
For streaming large responses, you can use the ``stream`` context manager:
161+
162+
.. code-block:: python
163+
164+
import httpx
165+
from python_proxy_headers import httpx_proxy
166+
167+
proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'})
168+
169+
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+
142179
Proxy Headers Overview
143180
----------------------
144181

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:
146183

147184
* **Control proxy behavior**: Send headers like ``X-ProxyMesh-Country`` to select a specific country for your proxy connection
148185
* **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
153190
httpcore Integration
154191
--------------------
155192

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.
157228

0 commit comments

Comments
 (0)