|
2 | 2 |
|
3 | 3 | !!! info "On this page" |
4 | 4 | - HTTP/HTTPS proxy |
5 | | - - Unix Socket proxy |
| 5 | + - Proxy with Authentication |
| 6 | + - Per-request proxy with custom headers |
| 7 | + - Unix Socket proxy for local services (Docker, Podman) |
| 8 | + - Constructor reference and choosing the right one |
6 | 9 |
|
7 | | -### HTTP/HTTPS Proxy |
| 10 | +The `Proxy` class provides several constructors depending on what you want to intercept: |
8 | 11 |
|
9 | | -Using proxies with authentication: |
| 12 | +| Constructor | What it proxies | |
| 13 | +|---|---| |
| 14 | +| `Proxy.all(url)` | All requests (HTTP + HTTPS) | |
| 15 | +| `Proxy.http(url)` | HTTP requests only | |
| 16 | +| `Proxy.https(url)` | HTTPS requests only | |
| 17 | +| `Proxy.unix(path)` | Requests via a Unix socket | |
| 18 | + |
| 19 | +You can pass a proxy in two ways: |
| 20 | +- **Per-client** via `Client(proxies=[...])` — applies to every request made by that client. |
| 21 | +- **Per-request** via `wreq.get(..., proxy=...)` — overrides or sets a proxy for a single request. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## HTTP / HTTPS Proxy |
| 26 | + |
| 27 | +### Basic usage with a client |
10 | 28 |
|
11 | 29 | ```python |
12 | 30 | import asyncio |
13 | | -import wreq |
14 | 31 | from wreq import Client, Proxy |
15 | 32 |
|
16 | | - |
17 | 33 | async def main(): |
18 | | - # Create a client with multiple proxies |
19 | 34 | client = Client( |
20 | | - proxies=[Proxy.http("socks5h://abc:def@127.0.0.1:6152")], |
| 35 | + proxies=[Proxy.all("http://proxy.example.com:8080")] |
21 | 36 | ) |
22 | 37 |
|
23 | | - # Send request via the client proxy |
24 | | - resp = await client.get("https://httpbin.io/anything") |
| 38 | + resp = await client.get("https://httpbin.io/ip") |
25 | 39 | print(await resp.text()) |
26 | 40 |
|
27 | | - # Send request via custom proxy |
| 41 | +asyncio.run(main()) |
| 42 | +``` |
| 43 | + |
| 44 | +All requests made through this `client` will be routed via the proxy. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### Proxy with authentication |
| 49 | + |
| 50 | +If your proxy requires credentials, include them directly in the URL using the `username:password@host:port` syntax. |
| 51 | + |
| 52 | +**HTTP / HTTPS proxy with credentials:** |
| 53 | + |
| 54 | +```python |
| 55 | +proxy = Proxy.all("http://username:password@proxy.example.com:8080") |
| 56 | + |
| 57 | +client = Client(proxies=[proxy]) |
| 58 | +``` |
| 59 | + |
| 60 | +Or using the credentials separately |
| 61 | + |
| 62 | +```python |
| 63 | +proxy = Proxy.all( |
| 64 | + url="http://proxy.example.com:8080", |
| 65 | + username="username", |
| 66 | + password="password" |
| 67 | +) |
| 68 | + |
| 69 | +client = Client(proxies=[proxy]) |
| 70 | +``` |
| 71 | + |
| 72 | +**SOCKS5 proxy with credentials:** |
| 73 | + |
| 74 | +```python |
| 75 | +# SOCKS5 |
| 76 | +client = Client( |
| 77 | + proxies=[Proxy.http("socks5://username:password@127.0.0.1:1080")] |
| 78 | +) |
| 79 | + |
| 80 | + |
| 81 | +# SOCKS5h (DNS also resolved by the proxy) |
| 82 | +client = Client( |
| 83 | + proxies=[Proxy.http("socks5h://username:password@127.0.0.1:6152")] |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +> The difference between `socks5://` and `socks5h://` is that `socks5h` delegates DNS resolution to the proxy server, which helps avoid DNS leaks. |
| 88 | +
|
| 89 | +--- |
| 90 | + |
| 91 | +### Per-request proxy with custom headers |
| 92 | + |
| 93 | +You can configure a proxy for a single request and attach custom headers sent to the proxy server: |
| 94 | + |
| 95 | +```python |
| 96 | +import asyncio |
| 97 | +import wreq |
| 98 | +from wreq import Proxy |
| 99 | + |
| 100 | +async def main(): |
28 | 101 | resp = await wreq.get( |
29 | 102 | "https://httpbin.io/anything", |
30 | | - proxy=Proxy.all( |
31 | | - url="http://127.0.0.1:6152", |
32 | | - custom_http_headers={ |
33 | | - "user-agent": "wreq", |
34 | | - "accept": "*/*", |
35 | | - "accept-encoding": "gzip, deflate, br", |
36 | | - "x-proxy": "wreq", |
37 | | - }, |
38 | | - ), |
| 103 | + proxies=[ |
| 104 | + Proxy.all( |
| 105 | + url="http://127.0.0.1:6152", |
| 106 | + custom_http_headers={ |
| 107 | + "user-agent": "wreq", |
| 108 | + "accept": "*/*", |
| 109 | + "accept-encoding": "gzip, deflate, br", |
| 110 | + "x-proxy": "wreq", |
| 111 | + }, |
| 112 | + ) |
| 113 | + ], |
39 | 114 | ) |
40 | 115 | print(await resp.text()) |
41 | 116 |
|
42 | | - |
43 | | -if __name__ == "__main__": |
44 | | - asyncio.run(main()) |
| 117 | +asyncio.run(main()) |
45 | 118 | ``` |
46 | 119 |
|
47 | | -### Unix Socket Proxy |
| 120 | +> **Note:** `custom_http_headers` are headers sent *to the proxy itself*, not to the final destination server. |
48 | 121 |
|
49 | | -Using Unix sockets for local services like Docker: |
| 122 | +--- |
| 123 | + |
| 124 | +## Unix Socket Proxy |
| 125 | + |
| 126 | +Unix sockets allow communication with local services without going through a network port. This is common when working with Docker, Podman, or other daemons that expose a socket file. |
50 | 127 |
|
51 | 128 | ```python |
52 | 129 | import asyncio |
53 | 130 | import wreq |
54 | 131 | from wreq import Proxy |
55 | 132 |
|
56 | | - |
57 | 133 | async def main(): |
58 | | - # Send request via Unix socket proxy |
59 | 134 | resp = await wreq.get( |
60 | 135 | "http://localhost/v1.41/containers/json", |
61 | | - proxy=Proxy.unix("/var/run/docker.sock"), |
| 136 | + proxies=[Proxy.unix("/var/run/docker.sock")], |
62 | 137 | ) |
63 | 138 | print(await resp.text()) |
64 | 139 |
|
65 | | - |
66 | | -if __name__ == "__main__": |
67 | | - asyncio.run(main()) |
| 140 | +asyncio.run(main()) |
68 | 141 | ``` |
| 142 | + |
| 143 | +Even though the URL says `http://localhost`, the request never touches the network. It goes directly through the socket file at the given path. |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Choosing the right constructor |
| 148 | + |
| 149 | +Each constructor controls which requests are intercepted by the proxy: |
| 150 | + |
| 151 | +| Constructor | Intercepts | |
| 152 | +|---|---| |
| 153 | +| `Proxy.all(url)` | All requests (HTTP + HTTPS) | |
| 154 | +| `Proxy.http(url)` | HTTP requests only | |
| 155 | +| `Proxy.https(url)` | HTTPS requests only | |
| 156 | +| `Proxy.unix(path)` | Requests via a Unix socket | |
0 commit comments