proxies.md
This file
Path
docs/advanced/proxies.md
83 lines · 3.5 KiB · Markdown
Last changed by
Revert "Removed leading $ from cli code blocks" (#3192)Tom Christiebefore adoptionf06171f
History of this fileTake it
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 1–65why, one step back1HTTPX supports setting up [HTTP proxies](https://en.wikipedia.org/wiki/Proxy_server#Web_proxy_servers) via the `proxy` parameter to be passed on client initialization or top-level API functions like `httpx.get(..., proxy=...)`.23<div align="center">4 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Open_proxy_h2g2bob.svg/480px-Open_proxy_h2g2bob.svg.png"/>5 <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>6</div>78## HTTP Proxies910To route all traffic (HTTP and HTTPS) to a proxy located at `http://localhost:8030`, pass the proxy URL to the client...1112```python13with httpx.Client(proxy="http://localhost:8030") as client:14 ...15```1617For more advanced use cases, pass a mounts `dict`. For example, 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:1819```python20proxy_mounts = {21 "http://": httpx.HTTPTransport(proxy="http://localhost:8030"),22 "https://": httpx.HTTPTransport(proxy="http://localhost:8031"),23}2425with httpx.Client(mounts=proxy_mounts) as client:26 ...27```2829For detailed information about proxy routing, see the [Routing](#routing) section.3031!!! tip "Gotcha"32 In most cases, the proxy URL for the `https://` key _should_ use the `http://` scheme (that's not a typo!).3334 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.3536 For more information, see [FORWARD vs TUNNEL](#forward-vs-tunnel).3738## Authentication3940Proxy credentials can be passed as the `userinfo` section of the proxy URL. For example:4142```python43with httpx.Client(proxy="http://username:password@localhost:8030") as client:44 ...45```4647## Proxy mechanisms4849!!! note50 This section describes **advanced** proxy concepts and functionality.5152### FORWARD vs TUNNEL5354In general, the flow for making an HTTP request through a proxy is as follows:55561. The client connects to the proxy (initial connection request).572. The proxy transfers data to the server on your behalf.5859How exactly step 2/ is performed depends on which of two proxying mechanisms is used:6061* **Forwarding**: the proxy makes the request for you, and sends back the response it obtained from the server.62* **Tunnelling**: 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).6364### Troubleshooting proxies65Fix sections references (#3058)Kar Petrosyanbefore adoption73e6888
line 66why, one step back66If you encounter issues when setting up proxies, please refer to our [Troubleshooting guide](../troubleshooting.md#proxies).Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 67–75why, one step back6768## SOCKS6970In addition to HTTP proxies, `httpcore` also supports proxies using the SOCKS protocol.71This is an optional feature that requires an additional third-party library be installed before use.7273You can install SOCKS support using `pip`:7475```shellRevert "Removed leading $ from cli code blocks" (#3192)Tom Christiebefore adoptionf06171f
line 76why, one step back76$ pip install httpx[socks]Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 77–83why, one step back77```7879You can now configure a client to make requests via a proxy using the SOCKS protocol:8081```python82httpx.Client(proxy='socks5://user:pass@host:port')83```