Skip to content

Commit b083389

Browse files
authored
docs: Enhance proxy usage documentation with the correct usage (#570)
1 parent 2efccfc commit b083389

2 files changed

Lines changed: 130 additions & 34 deletions

File tree

docs/source/getting-started/quickstart.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,21 @@ asyncio.run(main())
6969

7070
## Using Proxies
7171

72+
The [Proxy](../guide/proxy.md) object defines how your HTTP client routes traffic through a proxy server.
73+
You create one using a named constructor that specifies the scope:
74+
75+
- `Proxy.all(url)` - routes all traffic through the given proxy
76+
- `Proxy.http(url)` - only intercepts HTTP requests
77+
- `Proxy.https(url)` - only intercepts HTTPS requests
78+
79+
Once created, you pass it to the `Client` and all requests will go through it automatically.
80+
7281
```python
7382
import asyncio
74-
from wreq import Client
75-
from wreq.proxy import Proxy
83+
from wreq import Client, Proxy
7684

7785
async def main():
78-
client = Client(proxy=Proxy.all("http://proxy.example.com:8080"))
86+
client = Client(proxies=[Proxy.all("http://proxy.example.com:8080")])
7987
resp = await client.get("https://httpbin.org/ip")
8088
print(await resp.text())
8189

@@ -107,4 +115,4 @@ asyncio.run(main())
107115
## Next Steps
108116

109117
- See the [Examples](../guide/basic.md) for more code samples
110-
- Explore the [API Reference](../api/wreq.md) for detailed documentation
118+
- Explore the [API Reference](../api/wreq.md) for detailed documentation

docs/source/guide/proxy.md

Lines changed: 118 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,155 @@
22

33
!!! info "On this page"
44
- 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
69

7-
### HTTP/HTTPS Proxy
10+
The `Proxy` class provides several constructors depending on what you want to intercept:
811

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
1028

1129
```python
1230
import asyncio
13-
import wreq
1431
from wreq import Client, Proxy
1532

16-
1733
async def main():
18-
# Create a client with multiple proxies
1934
client = Client(
20-
proxies=[Proxy.http("socks5h://abc:def@127.0.0.1:6152")],
35+
proxies=[Proxy.all("http://proxy.example.com:8080")]
2136
)
2237

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")
2539
print(await resp.text())
2640

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():
28101
resp = await wreq.get(
29102
"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+
],
39114
)
40115
print(await resp.text())
41116

42-
43-
if __name__ == "__main__":
44-
asyncio.run(main())
117+
asyncio.run(main())
45118
```
46119

47-
### Unix Socket Proxy
120+
> **Note:** `custom_http_headers` are headers sent *to the proxy itself*, not to the final destination server.
48121
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.
50127

51128
```python
52129
import asyncio
53130
import wreq
54131
from wreq import Proxy
55132

56-
57133
async def main():
58-
# Send request via Unix socket proxy
59134
resp = await wreq.get(
60135
"http://localhost/v1.41/containers/json",
61-
proxy=Proxy.unix("/var/run/docker.sock"),
136+
proxies=[Proxy.unix("/var/run/docker.sock")],
62137
)
63138
print(await resp.text())
64139

65-
66-
if __name__ == "__main__":
67-
asyncio.run(main())
140+
asyncio.run(main())
68141
```
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

Comments
 (0)