Skip to content

Commit 2730d0d

Browse files
authored
Merge pull request #8 from proxymesh/feature/python-proxy-headers-examples
Add Python proxy header examples for all extensions
2 parents 3e9ee3e + 9fb0579 commit 2730d0d

12 files changed

Lines changed: 381 additions & 37 deletions

.cursorrules

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Cursor Rules for proxy-examples
2+
3+
## Git Configuration
4+
- Always commit as `cursor@proxymesh.com` with name `Cursor`
5+
- Run these commands if not already configured:
6+
```
7+
git config user.email "cursor@proxymesh.com"
8+
git config user.name "Cursor"
9+
```
10+
11+
## Pull Requests
12+
- Do NOT add "Made with Cursor" or similar footers to PR descriptions
13+
- After creating a PR with `gh pr create`, immediately run `gh pr edit <number> --body "..."` to ensure no footer was appended
14+
- Keep PR descriptions concise and focused on the changes
15+
16+
## Code Style
17+
- Follow existing code patterns in the repository
18+
- Keep examples simple and focused on demonstrating the feature
19+
- Include comments linking to documentation

README.md

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,52 @@ Example code for using proxy servers in different programming languages. Current
77

88
## Python Proxy Examples
99

10-
* [requests-proxy](https://github.com/proxymesh/proxy-examples/blob/main/python/requests-proxy.py)
11-
* [requests-random-proxy](https://github.com/proxymesh/proxy-examples/blob/main/python/requests-random-proxy.py)
12-
* [requests-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/requests-proxy-headers.py)
13-
* [urllib3-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/urllib3-proxy-headers.py)
14-
* [aiohttp-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/aiohttp-proxy-headers.py)
15-
* [httpx-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/httpx-proxy-headers.py)
16-
* [httpx-async-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/httpx-async-proxy-headers.py)
17-
* [scrapy-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/scrapy-proxy-headers.py)
10+
### Using python-proxy-headers
11+
12+
The [python-proxy-headers](https://github.com/proxymesh/python-proxy-headers) library enables sending custom headers to proxy servers and receiving proxy response headers. This is essential for services like [ProxyMesh](https://proxymesh.com) that use custom headers for country selection and IP assignment.
13+
14+
**Installation:**
15+
16+
```bash
17+
pip install python-proxy-headers
18+
```
19+
20+
**Examples:**
21+
22+
| Library | Example | Description |
23+
|---------|---------|-------------|
24+
| [requests](https://docs.python-requests.org/) | [requests-proxy-headers.py](python/requests-proxy-headers.py) | Simple HTTP requests with proxy headers |
25+
| requests | [requests-proxy-headers-session.py](python/requests-proxy-headers-session.py) | Session-based requests for connection pooling |
26+
| [urllib3](https://urllib3.readthedocs.io/) | [urllib3-proxy-headers.py](python/urllib3-proxy-headers.py) | Low-level HTTP client with proxy headers |
27+
| [aiohttp](https://docs.aiohttp.org/) | [aiohttp-proxy-headers.py](python/aiohttp-proxy-headers.py) | Async HTTP client with proxy headers |
28+
| [httpx](https://www.python-httpx.org/) | [httpx-proxy-headers.py](python/httpx-proxy-headers.py) | Modern HTTP client with proxy headers |
29+
| httpx | [httpx-async-proxy-headers.py](python/httpx-async-proxy-headers.py) | Async httpx with proxy headers |
30+
| [pycurl](http://pycurl.io/) | [pycurl-proxy-headers.py](python/pycurl-proxy-headers.py) | libcurl bindings with proxy headers |
31+
| pycurl | [pycurl-proxy-headers-lowlevel.py](python/pycurl-proxy-headers-lowlevel.py) | Low-level pycurl integration |
32+
| [cloudscraper](https://github.com/venomous/cloudscraper) | [cloudscraper-proxy-headers.py](python/cloudscraper-proxy-headers.py) | Cloudflare bypass with proxy headers |
33+
| [autoscraper](https://github.com/alirezamika/autoscraper) | [autoscraper-proxy-headers.py](python/autoscraper-proxy-headers.py) | Automatic web scraping with proxy headers |
34+
35+
### Basic Proxy Examples
36+
37+
* [requests-proxy.py](python/requests-proxy.py) - Basic proxy usage with requests
38+
* [requests-random-proxy.py](python/requests-random-proxy.py) - Random proxy rotation
39+
40+
### Scrapy
41+
42+
* [scrapy-proxy-headers.py](python/scrapy-proxy-headers.py) - Scrapy spider with proxy headers
1843

1944
## Ruby Proxy Examples
2045

21-
* [requests_proxy](https://github.com/proxymesh/proxy-examples/blob/main/ruby/requests_proxy.rb) from [rpolley](https://github.com/rpolley)
46+
* [requests_proxy.rb](ruby/requests_proxy.rb) - Ruby HTTP with proxy, from [rpolley](https://github.com/rpolley)
47+
48+
## Documentation
49+
50+
For more information on using proxy headers with Python:
51+
52+
* [python-proxy-headers on PyPI](https://pypi.org/project/python-proxy-headers/)
53+
* [python-proxy-headers Documentation](https://python-proxy-headers.readthedocs.io/)
54+
* [GitHub Repository](https://github.com/proxymesh/python-proxy-headers)
2255

2356
## Contributing
2457

25-
If you have example code for another language, please share it with a Pull Request.
58+
If you have example code for another language, please share it with a Pull Request.

python/aiohttp-proxy-headers.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1-
# See https://github.com/proxymesh/python-proxy-headers
1+
#!/usr/bin/env python3
2+
"""
3+
aiohttp with proxy headers example.
4+
5+
This example shows how to send custom headers to a proxy server and
6+
receive proxy response headers using the python-proxy-headers library
7+
with async/await.
8+
9+
See: https://github.com/proxymesh/python-proxy-headers
10+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/aiohttp.html
11+
"""
12+
import asyncio
213
from python_proxy_headers import aiohttp_proxy
314

4-
async with aiohttp_proxy.ProxyClientSession() as session:
5-
async with session.get('https://api.ipify.org?format=json', proxy="http://PROXYHOST:PORT", proxy_headers={'X-ProxyMesh-Country': 'US'}) as r:
6-
await r.text()
715

8-
r.headers['X-ProxyMesh-IP']
16+
async def main():
17+
async with aiohttp_proxy.ProxyClientSession() as session:
18+
async with session.get(
19+
'https://api.ipify.org?format=json',
20+
proxy='http://USERNAME:PASSWORD@PROXYHOST:PORT',
21+
proxy_headers={'X-ProxyMesh-Country': 'US'}
22+
) as response:
23+
# Print response
24+
print(f"Status: {response.status}")
25+
body = await response.text()
26+
print(f"Body: {body}")
27+
28+
# Access proxy response headers (from CONNECT response)
29+
proxy_ip = response.headers.get('X-ProxyMesh-IP')
30+
print(f"Proxy IP: {proxy_ip}")
31+
32+
33+
if __name__ == '__main__':
34+
asyncio.run(main())
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
"""
3+
AutoScraper with proxy headers example.
4+
5+
This example shows how to use AutoScraper with custom proxy headers.
6+
AutoScraper automatically learns scraping rules while python-proxy-headers
7+
enables sending custom headers to the proxy server.
8+
9+
See: https://github.com/proxymesh/python-proxy-headers
10+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/autoscraper.html
11+
"""
12+
from python_proxy_headers.autoscraper_proxy import ProxyAutoScraper
13+
14+
# Create an AutoScraper with proxy header support
15+
scraper = ProxyAutoScraper(proxy_headers={'X-ProxyMesh-Country': 'US'})
16+
17+
# Proxy configuration
18+
proxies = {
19+
'http': 'http://USERNAME:PASSWORD@PROXYHOST:PORT',
20+
'https': 'http://USERNAME:PASSWORD@PROXYHOST:PORT'
21+
}
22+
23+
# Build scraping rules from a sample page
24+
# AutoScraper learns what to extract based on the wanted_list
25+
result = scraper.build(
26+
url='https://quotes.toscrape.com/',
27+
wanted_list=['The world as we have created it is a process of our thinking.'],
28+
request_args={'proxies': proxies}
29+
)
30+
31+
print(f"Found {len(result)} matching quotes")
32+
for quote in result[:5]:
33+
print(f" - {quote[:60]}...")
34+
35+
# Save learned rules for later use
36+
# scraper.save('quotes_rules.json')
37+
38+
# Use learned rules on another page
39+
# result = scraper.get_result_similar(
40+
# url='https://quotes.toscrape.com/page/2/',
41+
# request_args={'proxies': proxies}
42+
# )
43+
44+
scraper.close()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
"""
3+
CloudScraper with proxy headers example.
4+
5+
This example shows how to use CloudScraper with custom proxy headers.
6+
CloudScraper bypasses Cloudflare protection while python-proxy-headers
7+
enables sending/receiving custom proxy headers.
8+
9+
See: https://github.com/proxymesh/python-proxy-headers
10+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/cloudscraper.html
11+
"""
12+
from python_proxy_headers.cloudscraper_proxy import create_scraper
13+
14+
# Create a CloudScraper with proxy header support
15+
scraper = create_scraper(
16+
proxy_headers={'X-ProxyMesh-Country': 'US'},
17+
browser='chrome'
18+
)
19+
20+
# Set proxy
21+
scraper.proxies = {
22+
'http': 'http://USERNAME:PASSWORD@PROXYHOST:PORT',
23+
'https': 'http://USERNAME:PASSWORD@PROXYHOST:PORT'
24+
}
25+
26+
# Make request - Cloudflare bypass + proxy headers work together
27+
response = scraper.get('https://api.ipify.org?format=json')
28+
29+
# Print response
30+
print(f"Status: {response.status_code}")
31+
print(f"Body: {response.text}")
32+
33+
# Access proxy response headers
34+
proxy_ip = response.headers.get('X-ProxyMesh-IP')
35+
print(f"Proxy IP: {proxy_ip}")
Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1+
#!/usr/bin/env python3
2+
"""
3+
httpx async with proxy headers example.
4+
5+
This example shows how to send custom headers to a proxy server and
6+
receive proxy response headers using httpx's async client.
7+
8+
See: https://github.com/proxymesh/python-proxy-headers
9+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/httpx.html
10+
"""
11+
import asyncio
112
import httpx
2-
# See https://github.com/proxymesh/python-proxy-headers
313
from python_proxy_headers.httpx_proxy import AsyncHTTPProxyTransport
414

5-
proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'})
6-
transport = AsyncHTTPProxyTransport(proxy=proxy)
715

8-
async with httpx.AsyncClient(mounts={'http://': transport, 'https://': transport}) as client:
9-
r = await client.get('https://api.ipify.org?format=json')
16+
async def main():
17+
# Create a proxy with custom headers
18+
proxy = httpx.Proxy(
19+
'http://USERNAME:PASSWORD@PROXYHOST:PORT',
20+
headers={'X-ProxyMesh-Country': 'US'}
21+
)
22+
23+
# Create async transport with proxy header support
24+
transport = AsyncHTTPProxyTransport(proxy=proxy)
25+
26+
async with httpx.AsyncClient(mounts={'http://': transport, 'https://': transport}) as client:
27+
response = await client.get('https://api.ipify.org?format=json')
28+
29+
# Print response
30+
print(f"Status: {response.status_code}")
31+
print(f"Body: {response.text}")
32+
33+
# Access proxy response headers (from CONNECT response)
34+
proxy_ip = response.headers.get('X-ProxyMesh-IP')
35+
print(f"Proxy IP: {proxy_ip}")
36+
1037

11-
r.headers['X-ProxyMesh-IP']
38+
if __name__ == '__main__':
39+
asyncio.run(main())

python/httpx-proxy-headers.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1+
#!/usr/bin/env python3
2+
"""
3+
httpx with proxy headers example.
4+
5+
This example shows how to send custom headers to a proxy server and
6+
receive proxy response headers using the python-proxy-headers library.
7+
8+
See: https://github.com/proxymesh/python-proxy-headers
9+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/httpx.html
10+
"""
111
import httpx
2-
# See https://github.com/proxymesh/python-proxy-headers
312
from python_proxy_headers import httpx_proxy
413

5-
proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'})
6-
r = httpx_proxy.get('https://api.ipify.org?format=json', proxy=proxy)
7-
r.headers['X-ProxyMesh-IP']
14+
# Create a proxy with custom headers
15+
proxy = httpx.Proxy(
16+
'http://USERNAME:PASSWORD@PROXYHOST:PORT',
17+
headers={'X-ProxyMesh-Country': 'US'}
18+
)
19+
20+
# Make a request using the helper function
21+
response = httpx_proxy.get('https://api.ipify.org?format=json', proxy=proxy)
22+
23+
# Print response
24+
print(f"Status: {response.status_code}")
25+
print(f"Body: {response.text}")
26+
27+
# Access proxy response headers (from CONNECT response)
28+
proxy_ip = response.headers.get('X-ProxyMesh-IP')
29+
print(f"Proxy IP: {proxy_ip}")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
"""
3+
PycURL with proxy headers - low-level example.
4+
5+
This example shows how to add proxy header support to existing pycurl code
6+
using the low-level helper functions from python-proxy-headers.
7+
8+
See: https://github.com/proxymesh/python-proxy-headers
9+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/pycurl.html
10+
"""
11+
import pycurl
12+
from io import BytesIO
13+
from python_proxy_headers.pycurl_proxy import set_proxy_headers, HeaderCapture
14+
15+
# Create a pycurl handle
16+
c = pycurl.Curl()
17+
buffer = BytesIO()
18+
19+
# Configure the request
20+
c.setopt(pycurl.URL, 'https://api.ipify.org?format=json')
21+
c.setopt(pycurl.PROXY, 'http://USERNAME:PASSWORD@PROXYHOST:PORT')
22+
c.setopt(pycurl.WRITEDATA, buffer)
23+
24+
# Add custom headers to send to the proxy
25+
set_proxy_headers(c, {'X-ProxyMesh-Country': 'US'})
26+
27+
# Capture response headers (installs HEADERFUNCTION callback)
28+
capture = HeaderCapture(c)
29+
30+
# Perform the request
31+
c.perform()
32+
33+
# Print results
34+
print(f"Status: {c.getinfo(pycurl.RESPONSE_CODE)}")
35+
print(f"Body: {buffer.getvalue().decode('utf-8')}")
36+
37+
# Access headers from the proxy's CONNECT response
38+
print(f"Proxy headers: {capture.proxy_headers}")
39+
print(f"Proxy IP: {capture.proxy_headers.get('X-ProxyMesh-IP')}")
40+
41+
# Access headers from the origin server
42+
print(f"Origin headers: {capture.origin_headers}")
43+
44+
c.close()

python/pycurl-proxy-headers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
"""
3+
PycURL with proxy headers example.
4+
5+
This example shows how to send custom headers to a proxy server and
6+
receive proxy response headers using the python-proxy-headers library.
7+
8+
See: https://github.com/proxymesh/python-proxy-headers
9+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/pycurl.html
10+
"""
11+
from python_proxy_headers.pycurl_proxy import get
12+
13+
# Make a request through the proxy with custom headers
14+
response = get(
15+
'https://api.ipify.org?format=json',
16+
proxy='http://USERNAME:PASSWORD@PROXYHOST:PORT',
17+
proxy_headers={'X-ProxyMesh-Country': 'US'}
18+
)
19+
20+
# Print response
21+
print(f"Status: {response.status_code}")
22+
print(f"Body: {response.text}")
23+
24+
# Access proxy response headers (from CONNECT response)
25+
proxy_ip = response.proxy_headers.get('X-ProxyMesh-IP')
26+
print(f"Proxy IP: {proxy_ip}")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Requests with proxy headers - Session example.
4+
5+
This example shows how to use ProxySession for connection pooling
6+
and making multiple requests with the same proxy header configuration.
7+
8+
See: https://github.com/proxymesh/python-proxy-headers
9+
Docs: https://python-proxy-headers.readthedocs.io/en/latest/requests.html
10+
"""
11+
from python_proxy_headers.requests_adapter import ProxySession
12+
13+
proxies = {
14+
'http': 'http://USERNAME:PASSWORD@PROXYHOST:PORT',
15+
'https': 'http://USERNAME:PASSWORD@PROXYHOST:PORT'
16+
}
17+
18+
# Create a session with proxy header support
19+
with ProxySession(proxy_headers={'X-ProxyMesh-Country': 'US'}) as session:
20+
session.proxies = proxies
21+
22+
# Make multiple requests with the same session
23+
r1 = session.get('https://api.ipify.org?format=json')
24+
print(f"Request 1 - IP: {r1.json()['ip']}, Proxy IP: {r1.headers.get('X-ProxyMesh-IP')}")
25+
26+
r2 = session.get('https://httpbin.org/headers')
27+
print(f"Request 2 - Status: {r2.status_code}")

0 commit comments

Comments
 (0)