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
Provenance
- Commit
70cdd95- Tree
5a53776- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
0e7730b
2 files changed+206 added−44 removed
modified:docs/advanced.md+204 added-44 removed
260 unmodified lines261 ```262 263 ## HTTP Proxying264 265-HTTPX supports setting up HTTP proxies the same way that Requests does via the `proxies` parameter.266-For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic267-to `http://127.0.0.1:3081` your `proxies` config would look like this:265+HTTPX 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.266 267+_Note: SOCKS proxies are not supported yet._268+269+<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>273+274+### Example275+276+To route all traffic (HTTP and HTTPS) to a proxy located at `http://localhost:8030`, pass the proxy URL to the client...277+278 ```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-... ...279+with httpx.Client(proxies="http://localhost:8030") as client:280+ ...281 ```282 278-Credentials may be passed in as part of the URL in the standard way, i.e.279-`http://username:password@127.0.0.1:3080`.283+To 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:284 281-Proxies can be configured for a specific scheme and host, all schemes of a host,282-all hosts for a scheme, or for all requests. When determining which proxy configuration283-to use for a given request this same order is used.285+```python286+proxies = {287+ "http": "http://localhost:8030",288+ "https": "http://localhost:8031",289+}290 291+with httpx.Client(proxies=proxies) as client:292+ ...293+```294+295+For detailed information about proxy routing, see the [Routing](#routing) section.296+297+!!! tip "Gotcha"298+ In most cases, the proxy URL for the `https` key _should_ use the `http://` scheme (that's not a typo!).299+300+ 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.301+302+ For more information, see [FORWARD vs TUNNEL](#forward-vs-tunnel).303+304+### Authentication305+306+Proxy credentials can be passed as the `userinfo` section of the proxy URL. For example:307+308 ```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-... ...309+proxies = {310+ "http": "http://username:password@localhost:8030",311+ # ...312+}313 ```314 300-!!! 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+### Routing316 305-By default `httpx.Proxy` will operate as a forwarding proxy for `http://...` requests306-and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change307-regardless of the proxy `url` being `http` or `https`.317+HTTPX 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.318 309-Proxies can be configured to have different behavior such as forwarding or tunneling all requests:319+#### Wildcard routing320 321+Route everything through a proxy...322+323 ```python312-proxy = httpx.Proxy(313- url="https://127.0.0.1",314- mode="TUNNEL_ONLY" # May be "TUNNEL_ONLY" or "FORWARD_ONLY". Defaults to "DEFAULT".315-)316-with httpx.Client(proxies=proxy) as client:317- # This request will be tunneled instead of forwarded.318- r = client.get("http://example.com")324+proxies = {325+ "all": "http://localhost:8030",326+}327 ```328 329+(This is equivalent to `Client(..., proxies="http://localhost:8030")`.)330+331+#### Scheme routing332+333+Route HTTP requests through one proxy, and HTTPS requests through another...334+335+```python336+proxies = {337+ "http": "http://localhost:8030",338+ "https": "http://localhost:8031",339+}340+```341+342+#### Domain routing343+344+Proxy all requests on domain "example.com", let other requests pass through...345+346+```python347+proxies = {348+ "all://example.com": "http://localhost:8030",349+}350+```351+352+Proxy HTTP requests on domain "example.com", let HTTPS and other requests pass through...353+354+```python355+proxies = {356+ "http://example.com": "http://localhost:8030",357+}358+```359+360+Proxy all requests to "example.com" and its subdomains, let other requests pass through...361+362+```python363+proxies = {364+ "all://*example.com": "http://localhost:8030",365+}366+```367+368+Proxy all requests to strict subdomains of "example.com", let "example.com" and other requests pass through...369+370+```python371+proxies = {372+ "all://*.example.com": "http://localhost:8030",373+}374+```375+376+#### Port routing377+378+Proxy HTTPS requests on port 1234 to "example.com"...379+380+```python381+proxies = {382+ "https://example.com:1234": "http://localhost:8030",383+}384+```385+386+Proxy all requests on port 1234...387+388+```python389+proxies = {390+ "all://*:1234": "http://localhost:8030",391+}392+```393+394+#### No-proxy support395+396+It is also possible to define requests that _shouldn't_ be routed through proxies.397+398+To do so, pass `None` as the proxy URL. For example...399+400+```python401+proxies = {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+```408+409+#### Complex configuration example410+411+You can combine the routing features outlined above to build complex proxy routing configurations. For example...412+413+```python414+proxies = {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+```426+427+#### Environment variables428+429+HTTP proxying can also be configured through environment variables, although with less fine-grained control.430+431+See documentation on [`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`](/environment_variables/#http_proxy-https_proxy-all_proxy) for more information.432+433+#### Routing algorithm overview434+435+For reference, the proxy routing algorithm used by HTTPX is roughly as follows...436+437+1. 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._438+1. 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.443+444+### Proxy mechanisms445+446 !!! note447+ This section describes **advanced** proxy concepts and functionality.448 323- 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 TUNNEL450 326-SOCKS proxies are *not* supported yet.451+In general, the flow for making an HTTP request through a proxy is as follows:452 453+1. The client connects to the proxy (initial connection request).454+1. The proxy somehow transfers data to the server on your behalf.455+456+How exactly step 2/ is performed depends on which of two proxying mechanisms is used:457+458+* **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).460+461+#### Default behavior462+463+Given the technical definitions above, by default (and regardless of whether you're using an HTTP or HTTPS proxy), HTTPX will:464+465+* Use forwarding for HTTP requests.466+* Use tunneling for HTTPS requests.467+468+This ensures that you can make HTTP and HTTPS requests in all cases (i.e. regardless of which type of proxy you're using).469+470+#### Forcing the proxy mechanism471+472+In most cases, the default behavior should work just fine as well as provide enough security.473+474+But 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...475+476+```python477+# Route all requests through an HTTPS proxy, using tunneling only.478+proxies = httpx.Proxy(479+ url="https://localhost:8030",480+ mode="TUNNEL_ONLY",481+)482+483+with httpx.Client(proxies=proxies) as client:484+ # This HTTP request will be tunneled instead of forwarded.485+ r = client.get("http://example.com")486+```487+488 ## Timeout Configuration489 490 HTTPX is careful to enforce timeouts everywhere by default.491 378 unmodified linesmodified:docs/environment_variables.md+2 added
150 unmodified lines151 152 # This request will be sent through the proxy153 python -c "import httpx; httpx.get('http://example.com')"154 ```155+156+For more information on using proxies in HTTPX, see [HTTP Proxying](/advanced/#http-proxying).