Skip to content
Myr

Revamp proxies documentation (#1123)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by GitHub · parent 0e7730b · Browse files · 2 files+206 added−44 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+206 added−44 removed

  • other:+206 added−44 removed

2 files+206 added−44 removed

modified:docs/advanced.md+204 added-44 removed
260 unmodified lines
261```262263## HTTP Proxying264265HTTPX supports setting up HTTP proxies the same way that Requests does via the `proxies` parameter.266For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic267to `http://127.0.0.1:3081` your `proxies` config would look like this:265HTTPX supports setting up [HTTP proxies](https://en.wikipedia.org/wiki/Proxy_server#Web_proxy_servers) via the `proxies` parameter to be passed on client initialization.266267_Note: SOCKS proxies are not supported yet._268269<div align="center">270 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Open_proxy_h2g2bob.svg/480px-Open_proxy_h2g2bob.svg.png"/>271 <figcaption><em>Diagram of how a proxy works (source: Wikipedia). The left hand side "Internet" blob may be your HTTPX client requesting <code>example.com</code> through a proxy.</em></figcaption>272</div>273274### Example275276To route all traffic (HTTP and HTTPS) to a proxy located at `http://localhost:8030`, pass the proxy URL to the client...277278```python270>>> proxies = {271... "http": "http://127.0.0.1:3080",272... "https": "http://127.0.0.1:3081"273... }274>>> with httpx.Client(proxies=proxies) as client:275... ...279with httpx.Client(proxies="http://localhost:8030") as client:280 ...281```282278Credentials may be passed in as part of the URL in the standard way, i.e.279`http://username:password@127.0.0.1:3080`.283To route HTTP and HTTPS requests to 2 different proxies, respectively located at `http://localhost:8030`, and `http://localhost:8031`, pass a `dict` of proxy URLs:284281Proxies can be configured for a specific scheme and host, all schemes of a host,282all hosts for a scheme, or for all requests. When determining which proxy configuration283to use for a given request this same order is used.285```python286proxies = {287 "http": "http://localhost:8030",288 "https": "http://localhost:8031",289}290291with httpx.Client(proxies=proxies) as client:292 ...293```294295For detailed information about proxy routing, see the [Routing](#routing) section.296297!!! tip "Gotcha"298 In most cases, the proxy URL for the `https` key _should_ use the `http://` scheme (that's not a typo!).299300 This is because HTTP proxying requires initiating a connection with the proxy server. While it's possible that your proxy supports doing it via HTTPS, most proxies only support doing it via HTTP.301302 For more information, see [FORWARD vs TUNNEL](#forward-vs-tunnel).303304### Authentication305306Proxy credentials can be passed as the `userinfo` section of the proxy URL. For example:307308```python286>>> proxies = {287... "http://example.com": "...", # Host+Scheme288... "all://example.com": "...", # Host289... "http": "...", # Scheme290... "all": "...", # All291... }292>>> with httpx.Client(proxies=proxies) as client:293... ...294...295>>> proxy = "..." # Shortcut for {'all': '...'}296>>> with httpx.Client(proxies=proxy) as client:297... ...309proxies = {310 "http": "http://username:password@localhost:8030",311 # ...312}313```314300!!! warning301 To make sure that proxies cannot read your traffic,302 and even if the proxy_url uses HTTPS, it is recommended to303 use HTTPS and tunnel requests if possible.315### Routing316305By default `httpx.Proxy` will operate as a forwarding proxy for `http://...` requests306and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change307regardless of the proxy `url` being `http` or `https`.317HTTPX supports configuring which requests go through a given proxy and which don't based on the requested **scheme**, **domain**, **port**, or a combination of these.318309Proxies can be configured to have different behavior such as forwarding or tunneling all requests:319#### Wildcard routing320321Route everything through a proxy...322323```python312proxy = httpx.Proxy(313 url="https://127.0.0.1",314 mode="TUNNEL_ONLY" # May be "TUNNEL_ONLY" or "FORWARD_ONLY". Defaults to "DEFAULT".315)316with httpx.Client(proxies=proxy) as client:317 # This request will be tunneled instead of forwarded.318 r = client.get("http://example.com")324proxies = {325 "all": "http://localhost:8030",326}327```328329(This is equivalent to `Client(..., proxies="http://localhost:8030")`.)330331#### Scheme routing332333Route HTTP requests through one proxy, and HTTPS requests through another...334335```python336proxies = {337 "http": "http://localhost:8030",338 "https": "http://localhost:8031",339}340```341342#### Domain routing343344Proxy all requests on domain "example.com", let other requests pass through...345346```python347proxies = {348 "all://example.com": "http://localhost:8030",349}350```351352Proxy HTTP requests on domain "example.com", let HTTPS and other requests pass through...353354```python355proxies = {356 "http://example.com": "http://localhost:8030",357}358```359360Proxy all requests to "example.com" and its subdomains, let other requests pass through...361362```python363proxies = {364 "all://*example.com": "http://localhost:8030",365}366```367368Proxy all requests to strict subdomains of "example.com", let "example.com" and other requests pass through...369370```python371proxies = {372 "all://*.example.com": "http://localhost:8030",373}374```375376#### Port routing377378Proxy HTTPS requests on port 1234 to "example.com"...379380```python381proxies = {382 "https://example.com:1234": "http://localhost:8030",383}384```385386Proxy all requests on port 1234...387388```python389proxies = {390 "all://*:1234": "http://localhost:8030",391}392```393394#### No-proxy support395396It is also possible to define requests that _shouldn't_ be routed through proxies.397398To do so, pass `None` as the proxy URL. For example...399400```python401proxies = {402 # Route requests through a proxy by default...403 "all": "http://localhost:8031",404 # Except those for "example.com".405 "all://example.com": None,406}407```408409#### Complex configuration example410411You can combine the routing features outlined above to build complex proxy routing configurations. For example...412413```python414proxies = {415 # Route all traffic through a proxy by default...416 "all": "http://localhost:8030",417 # But don't use proxies for HTTPS requests to "domain.io"...418 "https://domain.io": None,419 # And use another proxy for requests to "example.com" and its subdomains...420 "all://*example.com": "http://localhost:8031",421 # And yet another proxy if HTTP is used,422 # and the "internal" subdomain on port 5550 is requested...423 "http://internal.example.com:5550": "http://localhost:8032",424}425```426427#### Environment variables428429HTTP proxying can also be configured through environment variables, although with less fine-grained control.430431See documentation on [`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`](/environment_variables/#http_proxy-https_proxy-all_proxy) for more information.432433#### Routing algorithm overview434435For reference, the proxy routing algorithm used by HTTPX is roughly as follows...4364371. Sort proxy keys in decreasing order of specificity. (For example, a proxy on `all://example.com` comes before a proxy on `all`.) _This is done on client instanciation._4381. When making a request, go through sorted proxies, looking for a matching key...439 1. If a key matches...440 1. ...But the value is `None`, no proxy is used.441 1. Otherwise, the associated proxy is used.442 1. If no key matched, no proxy is used.443444### Proxy mechanisms445446!!! note447 This section describes **advanced** proxy concepts and functionality.448323 To use proxies you must pass the proxy information at `Client` initialization,324 rather than on the `.get(...)` call or other request methods.449#### FORWARD vs TUNNEL450326SOCKS proxies are *not* supported yet.451In general, the flow for making an HTTP request through a proxy is as follows:4524531. The client connects to the proxy (initial connection request).4541. The proxy somehow transfers data to the server on your behalf.455456How exactly step 2/ is performed depends on which of two proxying mechanisms is used:457458* **Forwarding**: the proxy makes the request for you, and sends back the response it obtained from the server.459* **Tunneling**: the proxy establishes a TCP connection to the server on your behalf, and the client reuses this connection to send the request and receive the response. This is known as an [HTTP Tunnel](https://en.wikipedia.org/wiki/HTTP_tunnel). This mechanism is how you can access websites that use HTTPS from an HTTP proxy (the client "upgrades" the connection to HTTPS by performing the TLS handshake with the server over the TCP connection provided by the proxy).460461#### Default behavior462463Given the technical definitions above, by default (and regardless of whether you're using an HTTP or HTTPS proxy), HTTPX will:464465* Use forwarding for HTTP requests.466* Use tunneling for HTTPS requests.467468This ensures that you can make HTTP and HTTPS requests in all cases (i.e. regardless of which type of proxy you're using).469470#### Forcing the proxy mechanism471472In most cases, the default behavior should work just fine as well as provide enough security.473474But if you know what you're doing and you want to force which mechanism to use, you can do so by passing an `httpx.Proxy()` instance, setting the `mode` to either `FORWARD_ONLY` or `TUNNEL_ONLY`. For example...475476```python477# Route all requests through an HTTPS proxy, using tunneling only.478proxies = httpx.Proxy(479 url="https://localhost:8030",480 mode="TUNNEL_ONLY",481)482483with httpx.Client(proxies=proxies) as client:484 # This HTTP request will be tunneled instead of forwarded.485 r = client.get("http://example.com")486```487488## Timeout Configuration489490HTTPX is careful to enforce timeouts everywhere by default.491
378 unmodified lines
modified:docs/environment_variables.md+2 added
150 unmodified lines
151152# This request will be sent through the proxy153python -c "import httpx; httpx.get('http://example.com')"154```155156For more information on using proxies in HTTPX, see [HTTP Proxying](/advanced/#http-proxying).