|
| 1 | +httpx |
| 2 | +===== |
| 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. |
| 5 | + |
| 6 | +Using Proxies with httpx |
| 7 | +------------------------ |
| 8 | + |
| 9 | +httpx provides built-in support for proxies through the ``httpx.Proxy`` class. You can create a proxy object and use it with httpx clients. |
| 10 | + |
| 11 | +Basic Proxy Usage |
| 12 | +~~~~~~~~~~~~~~~~~ |
| 13 | + |
| 14 | +httpx also supports proxy headers by default, though it's not documented. You can use the ``httpx.Proxy`` class with custom headers: |
| 15 | + |
| 16 | +.. code-block:: python |
| 17 | +
|
| 18 | + import httpx |
| 19 | + from httpx import HTTPProxyTransport |
| 20 | + proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'}) |
| 21 | + transport = HTTPProxyTransport(proxy=proxy) |
| 22 | + with httpx.Client(mounts={'http://': transport, 'https://': transport}) as client: |
| 23 | + r = client.get('https://api.ipify.org?format=json') |
| 24 | +
|
| 25 | +This creates a proxy with custom headers and uses it with an httpx client. |
| 26 | + |
| 27 | +Receiving Proxy Response Headers |
| 28 | +--------------------------------- |
| 29 | + |
| 30 | +But to get the response headers, you need to use our extension module ``python_proxy_headers.httpx_proxy``: |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + import httpx |
| 35 | + from python_proxy_headers.httpx_proxy import HTTPProxyTransport |
| 36 | + proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'}) |
| 37 | + transport = HTTPProxyTransport(proxy=proxy) |
| 38 | + with httpx.Client(mounts={'http://': transport, 'https://': transport}) as client: |
| 39 | + r = client.get('https://api.ipify.org?format=json') |
| 40 | + |
| 41 | + r.headers['X-ProxyMesh-IP'] |
| 42 | +
|
| 43 | +The ``HTTPProxyTransport`` from our extension module extends the standard transport to make proxy response headers available in the response headers. |
| 44 | + |
| 45 | +Helper Methods |
| 46 | +-------------- |
| 47 | + |
| 48 | +This module also provides helper methods similar to requests for convenience: |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + import httpx |
| 53 | + from python_proxy_headers import httpx_proxy |
| 54 | + proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'}) |
| 55 | + r = httpx_proxy.get('https://api.ipify.org?format=json', proxy=proxy) |
| 56 | + r.headers['X-ProxyMesh-IP'] |
| 57 | +
|
| 58 | +The helper module supports all standard HTTP methods: ``get``, ``post``, ``put``, ``delete``, ``patch``, ``head``, and ``options``. |
| 59 | + |
| 60 | +Async Support |
| 61 | +------------- |
| 62 | + |
| 63 | +httpx supports async requests, so we provide an async extension too: |
| 64 | + |
| 65 | +.. code-block:: python |
| 66 | +
|
| 67 | + import httpx |
| 68 | + from python_proxy_headers.httpx_proxy import AsyncHTTPProxyTransport |
| 69 | + proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'}) |
| 70 | + transport = AsyncHTTPProxyTransport(proxy=proxy) |
| 71 | + async with httpx.AsyncClient(mounts={'http://': transport, 'https://': transport}) as client: |
| 72 | + r = await client.get('https://api.ipify.org?format=json') |
| 73 | + |
| 74 | + r.headers['X-ProxyMesh-IP'] |
| 75 | +
|
| 76 | +The ``AsyncHTTPProxyTransport`` works just like the sync version but for async clients. |
| 77 | + |
| 78 | +Complete Examples |
| 79 | +----------------- |
| 80 | + |
| 81 | +Synchronous Example |
| 82 | +~~~~~~~~~~~~~~~~~~~ |
| 83 | + |
| 84 | +.. code-block:: python |
| 85 | +
|
| 86 | + import httpx |
| 87 | + from python_proxy_headers.httpx_proxy import HTTPProxyTransport |
| 88 | + |
| 89 | + proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'}) |
| 90 | + transport = HTTPProxyTransport(proxy=proxy) |
| 91 | + |
| 92 | + with httpx.Client(mounts={'http://': transport, 'https://': transport}) as client: |
| 93 | + r = client.get('https://api.ipify.org?format=json') |
| 94 | + data = r.json() |
| 95 | + proxy_ip = r.headers.get('X-ProxyMesh-IP') |
| 96 | + print(f"Your IP: {data['ip']}") |
| 97 | + print(f"Proxy IP: {proxy_ip}") |
| 98 | +
|
| 99 | +Asynchronous Example |
| 100 | +~~~~~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +.. code-block:: python |
| 103 | +
|
| 104 | + import httpx |
| 105 | + from python_proxy_headers.httpx_proxy import AsyncHTTPProxyTransport |
| 106 | + |
| 107 | + async def main(): |
| 108 | + proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'}) |
| 109 | + transport = AsyncHTTPProxyTransport(proxy=proxy) |
| 110 | + |
| 111 | + async with httpx.AsyncClient(mounts={'http://': transport, 'https://': transport}) as client: |
| 112 | + r = await client.get('https://api.ipify.org?format=json') |
| 113 | + data = r.json() |
| 114 | + proxy_ip = r.headers.get('X-ProxyMesh-IP') |
| 115 | + print(f"Your IP: {data['ip']}") |
| 116 | + print(f"Proxy IP: {proxy_ip}") |
| 117 | + |
| 118 | + import asyncio |
| 119 | + asyncio.run(main()) |
| 120 | +
|
| 121 | +Using Helper Methods |
| 122 | +~~~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +For simpler use cases, you can use the helper methods: |
| 125 | + |
| 126 | +.. code-block:: python |
| 127 | +
|
| 128 | + import httpx |
| 129 | + from python_proxy_headers import httpx_proxy |
| 130 | + |
| 131 | + proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'}) |
| 132 | + |
| 133 | + # GET request |
| 134 | + r = httpx_proxy.get('https://api.example.com', proxy=proxy) |
| 135 | + |
| 136 | + # POST request |
| 137 | + r = httpx_proxy.post('https://api.example.com', json={'key': 'value'}, proxy=proxy) |
| 138 | + |
| 139 | + # Access proxy response headers |
| 140 | + proxy_ip = r.headers.get('X-ProxyMesh-IP') |
| 141 | +
|
| 142 | +Proxy Headers Overview |
| 143 | +---------------------- |
| 144 | + |
| 145 | +Proxy headers are custom HTTP headers that can be used to communicate with proxy servers. They allow you to: |
| 146 | + |
| 147 | +* **Control proxy behavior**: Send headers like ``X-ProxyMesh-Country`` to select a specific country for your proxy connection |
| 148 | +* **Receive proxy information**: Get headers like ``X-ProxyMesh-IP`` to know which IP address was assigned to your request |
| 149 | +* **Maintain session consistency**: Use headers like ``X-ProxyMesh-IP`` to ensure you get the same IP address across multiple requests |
| 150 | + |
| 151 | +The exact headers available depend on your proxy provider. Check your proxy provider's documentation for the specific headers they support. |
| 152 | + |
| 153 | +httpcore Integration |
| 154 | +-------------------- |
| 155 | + |
| 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. |
| 157 | + |
0 commit comments