Skip to content

Commit 742807c

Browse files
authored
Merge pull request #10 from proxymesh/feature/env-var-config
Update examples to use environment variables for proxy config
2 parents 2730d0d + 0ea6593 commit 742807c

15 files changed

Lines changed: 498 additions & 192 deletions

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,34 @@ The [python-proxy-headers](https://github.com/proxymesh/python-proxy-headers) li
1717
pip install python-proxy-headers
1818
```
1919

20+
**Running Examples:**
21+
22+
All examples read proxy configuration from environment variables:
23+
24+
```bash
25+
# Required: Set your proxy URL
26+
export PROXY_URL='http://user:pass@proxy.example.com:8080'
27+
28+
# Optional: Custom test URL (default: https://api.ipify.org?format=json)
29+
export TEST_URL='https://httpbin.org/ip'
30+
31+
# Optional: Send a custom header to the proxy
32+
export PROXY_HEADER='X-ProxyMesh-Country'
33+
export PROXY_VALUE='US'
34+
35+
# Optional: Read a specific header from the response
36+
export RESPONSE_HEADER='X-ProxyMesh-IP'
37+
38+
# Run a single example
39+
python python/requests-proxy-headers.py
40+
41+
# Run all examples as tests
42+
python python/run_tests.py
43+
44+
# Run specific examples
45+
python python/run_tests.py requests-proxy-headers httpx-proxy-headers
46+
```
47+
2048
**Examples:**
2149

2250
| Library | Example | Description |

python/aiohttp-proxy-headers.py

100644100755
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,46 @@
22
"""
33
aiohttp with proxy headers example.
44
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.
5+
Configuration via environment variables:
6+
PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
7+
TEST_URL - URL to request (default: https://api.ipify.org?format=json)
8+
PROXY_HEADER - Header name to send to proxy (optional)
9+
PROXY_VALUE - Header value to send to proxy (optional)
10+
RESPONSE_HEADER - Header name to read from response (optional)
811
912
See: https://github.com/proxymesh/python-proxy-headers
10-
Docs: https://python-proxy-headers.readthedocs.io/en/latest/aiohttp.html
1113
"""
14+
import os
15+
import sys
1216
import asyncio
1317
from python_proxy_headers import aiohttp_proxy
1418

19+
# Get configuration from environment
20+
proxy_url = os.environ.get('PROXY_URL') or os.environ.get('HTTPS_PROXY')
21+
if not proxy_url:
22+
print("Error: Set PROXY_URL environment variable", file=sys.stderr)
23+
sys.exit(1)
24+
25+
test_url = os.environ.get('TEST_URL', 'https://api.ipify.org?format=json')
26+
proxy_header = os.environ.get('PROXY_HEADER')
27+
proxy_value = os.environ.get('PROXY_VALUE')
28+
response_header = os.environ.get('RESPONSE_HEADER')
29+
30+
proxy_headers = {proxy_header: proxy_value} if proxy_header and proxy_value else None
31+
1532

1633
async def main():
1734
async with aiohttp_proxy.ProxyClientSession() as session:
1835
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'}
36+
test_url,
37+
proxy=proxy_url,
38+
proxy_headers=proxy_headers
2239
) as response:
23-
# Print response
24-
print(f"Status: {response.status}")
2540
body = await response.text()
41+
print(f"Status: {response.status}")
2642
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}")
43+
if response_header:
44+
print(f"{response_header}: {response.headers.get(response_header)}")
3145

3246

3347
if __name__ == '__main__':

python/autoscraper-proxy-headers.py

100644100755
Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,44 @@
22
"""
33
AutoScraper with proxy headers example.
44
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.
5+
Configuration via environment variables:
6+
PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
7+
TEST_URL - URL to request (default: https://api.ipify.org?format=json)
8+
PROXY_HEADER - Header name to send to proxy (optional)
9+
PROXY_VALUE - Header value to send to proxy (optional)
10+
RESPONSE_HEADER - Header name to read from response (optional)
811
912
See: https://github.com/proxymesh/python-proxy-headers
10-
Docs: https://python-proxy-headers.readthedocs.io/en/latest/autoscraper.html
1113
"""
14+
import os
15+
import sys
1216
from python_proxy_headers.autoscraper_proxy import ProxyAutoScraper
1317

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-
# )
18+
# Get configuration from environment
19+
proxy_url = os.environ.get('PROXY_URL') or os.environ.get('HTTPS_PROXY')
20+
if not proxy_url:
21+
print("Error: Set PROXY_URL environment variable", file=sys.stderr)
22+
sys.exit(1)
23+
24+
test_url = os.environ.get('TEST_URL', 'https://api.ipify.org?format=json')
25+
proxy_header = os.environ.get('PROXY_HEADER')
26+
proxy_value = os.environ.get('PROXY_VALUE')
27+
response_header = os.environ.get('RESPONSE_HEADER')
28+
29+
proxy_headers = {proxy_header: proxy_value} if proxy_header and proxy_value else None
30+
31+
# Create scraper and test via underlying session
32+
scraper = ProxyAutoScraper(proxy_headers=proxy_headers)
33+
session = scraper._get_session()
34+
session.proxies = {'http': proxy_url, 'https': proxy_url}
35+
36+
# Make request
37+
response = session.get(test_url)
38+
39+
# Output
40+
print(f"Status: {response.status_code}")
41+
print(f"Body: {response.text}")
42+
if response_header:
43+
print(f"{response_header}: {response.headers.get(response_header)}")
4344

4445
scraper.close()

python/cloudscraper-proxy-headers.py

100644100755
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,41 @@
22
"""
33
CloudScraper with proxy headers example.
44
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.
5+
Configuration via environment variables:
6+
PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
7+
TEST_URL - URL to request (default: https://api.ipify.org?format=json)
8+
PROXY_HEADER - Header name to send to proxy (optional)
9+
PROXY_VALUE - Header value to send to proxy (optional)
10+
RESPONSE_HEADER - Header name to read from response (optional)
811
912
See: https://github.com/proxymesh/python-proxy-headers
10-
Docs: https://python-proxy-headers.readthedocs.io/en/latest/cloudscraper.html
1113
"""
14+
import os
15+
import sys
1216
from python_proxy_headers.cloudscraper_proxy import create_scraper
1317

14-
# Create a CloudScraper with proxy header support
15-
scraper = create_scraper(
16-
proxy_headers={'X-ProxyMesh-Country': 'US'},
17-
browser='chrome'
18-
)
18+
# Get configuration from environment
19+
proxy_url = os.environ.get('PROXY_URL') or os.environ.get('HTTPS_PROXY')
20+
if not proxy_url:
21+
print("Error: Set PROXY_URL environment variable", file=sys.stderr)
22+
sys.exit(1)
1923

20-
# Set proxy
21-
scraper.proxies = {
22-
'http': 'http://USERNAME:PASSWORD@PROXYHOST:PORT',
23-
'https': 'http://USERNAME:PASSWORD@PROXYHOST:PORT'
24-
}
24+
test_url = os.environ.get('TEST_URL', 'https://api.ipify.org?format=json')
25+
proxy_header = os.environ.get('PROXY_HEADER')
26+
proxy_value = os.environ.get('PROXY_VALUE')
27+
response_header = os.environ.get('RESPONSE_HEADER')
2528

26-
# Make request - Cloudflare bypass + proxy headers work together
27-
response = scraper.get('https://api.ipify.org?format=json')
29+
proxy_headers = {proxy_header: proxy_value} if proxy_header and proxy_value else None
2830

29-
# Print response
31+
# Create scraper with proxy headers
32+
scraper = create_scraper(proxy_headers=proxy_headers, browser='chrome')
33+
scraper.proxies = {'http': proxy_url, 'https': proxy_url}
34+
35+
# Make request
36+
response = scraper.get(test_url)
37+
38+
# Output
3039
print(f"Status: {response.status_code}")
3140
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}")
41+
if response_header:
42+
print(f"{response_header}: {response.headers.get(response_header)}")

python/httpx-async-proxy-headers.py

100644100755
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,51 @@
22
"""
33
httpx async with proxy headers example.
44
5-
This example shows how to send custom headers to a proxy server and
6-
receive proxy response headers using httpx's async client.
5+
Configuration via environment variables:
6+
PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
7+
TEST_URL - URL to request (default: https://api.ipify.org?format=json)
8+
PROXY_HEADER - Header name to send to proxy (optional)
9+
PROXY_VALUE - Header value to send to proxy (optional)
10+
RESPONSE_HEADER - Header name to read from response (optional)
711
812
See: https://github.com/proxymesh/python-proxy-headers
9-
Docs: https://python-proxy-headers.readthedocs.io/en/latest/httpx.html
1013
"""
14+
import os
15+
import sys
1116
import asyncio
1217
import httpx
1318
from python_proxy_headers.httpx_proxy import AsyncHTTPProxyTransport
1419

20+
# Get configuration from environment
21+
proxy_url = os.environ.get('PROXY_URL') or os.environ.get('HTTPS_PROXY')
22+
if not proxy_url:
23+
print("Error: Set PROXY_URL environment variable", file=sys.stderr)
24+
sys.exit(1)
25+
26+
test_url = os.environ.get('TEST_URL', 'https://api.ipify.org?format=json')
27+
proxy_header = os.environ.get('PROXY_HEADER')
28+
proxy_value = os.environ.get('PROXY_VALUE')
29+
response_header = os.environ.get('RESPONSE_HEADER')
30+
31+
proxy_headers = {proxy_header: proxy_value} if proxy_header and proxy_value else None
32+
1533

1634
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-
)
35+
# Create proxy with optional headers
36+
if proxy_headers:
37+
proxy = httpx.Proxy(proxy_url, headers=proxy_headers)
38+
else:
39+
proxy = proxy_url
2240

23-
# Create async transport with proxy header support
2441
transport = AsyncHTTPProxyTransport(proxy=proxy)
2542

2643
async with httpx.AsyncClient(mounts={'http://': transport, 'https://': transport}) as client:
27-
response = await client.get('https://api.ipify.org?format=json')
44+
response = await client.get(test_url)
2845

29-
# Print response
3046
print(f"Status: {response.status_code}")
3147
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}")
48+
if response_header:
49+
print(f"{response_header}: {response.headers.get(response_header)}")
3650

3751

3852
if __name__ == '__main__':

python/httpx-proxy-headers.py

100644100755
Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,44 @@
22
"""
33
httpx with proxy headers example.
44
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.
5+
Configuration via environment variables:
6+
PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
7+
TEST_URL - URL to request (default: https://api.ipify.org?format=json)
8+
PROXY_HEADER - Header name to send to proxy (optional)
9+
PROXY_VALUE - Header value to send to proxy (optional)
10+
RESPONSE_HEADER - Header name to read from response (optional)
711
812
See: https://github.com/proxymesh/python-proxy-headers
9-
Docs: https://python-proxy-headers.readthedocs.io/en/latest/httpx.html
1013
"""
14+
import os
15+
import sys
1116
import httpx
1217
from python_proxy_headers import httpx_proxy
1318

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+
# Get configuration from environment
20+
proxy_url = os.environ.get('PROXY_URL') or os.environ.get('HTTPS_PROXY')
21+
if not proxy_url:
22+
print("Error: Set PROXY_URL environment variable", file=sys.stderr)
23+
sys.exit(1)
1924

20-
# Make a request using the helper function
21-
response = httpx_proxy.get('https://api.ipify.org?format=json', proxy=proxy)
25+
test_url = os.environ.get('TEST_URL', 'https://api.ipify.org?format=json')
26+
proxy_header = os.environ.get('PROXY_HEADER')
27+
proxy_value = os.environ.get('PROXY_VALUE')
28+
response_header = os.environ.get('RESPONSE_HEADER')
2229

23-
# Print response
30+
proxy_headers = {proxy_header: proxy_value} if proxy_header and proxy_value else None
31+
32+
# Create proxy with optional headers
33+
if proxy_headers:
34+
proxy = httpx.Proxy(proxy_url, headers=proxy_headers)
35+
else:
36+
proxy = proxy_url
37+
38+
# Make request
39+
response = httpx_proxy.get(test_url, proxy=proxy)
40+
41+
# Output
2442
print(f"Status: {response.status_code}")
2543
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}")
44+
if response_header:
45+
print(f"{response_header}: {response.headers.get(response_header)}")

0 commit comments

Comments
 (0)