Handle custom proxy headers for http & https requests in various python libraries
Project description
Python Proxy Headers
Extensions for Python HTTP libraries to support sending and receiving custom proxy headers during HTTPS CONNECT tunneling.
The Problem
When making HTTPS requests through a proxy, the connection is established via a CONNECT tunnel. During this process:
-
Sending headers to the proxy - Most Python HTTP libraries don't provide an easy way to send custom headers (like
X-ProxyMesh-Country) to the proxy server during the CONNECT handshake. -
Receiving headers from the proxy - The proxy's response headers from the CONNECT request are typically discarded, making it impossible to read custom headers (like
X-ProxyMesh-IP) that the proxy sends back.
This library solves both problems for popular Python HTTP libraries.
Supported Libraries
| Library | Module | Use Case |
|---|---|---|
| urllib3 | urllib3_proxy_manager |
Low-level HTTP client |
| requests | requests_adapter |
Simple HTTP requests |
| aiohttp | aiohttp_proxy |
Async HTTP client |
| httpx | httpx_proxy |
Modern HTTP client |
| pycurl | pycurl_proxy |
libcurl bindings |
| cloudscraper | cloudscraper_proxy |
Cloudflare bypass |
| autoscraper | autoscraper_proxy |
Automatic web scraping |
Installation
pip install python-proxy-headers
Then install the HTTP library you want to use (e.g., pip install requests).
Note: This package has no dependencies by default - install only what you need.
Quick Start
requests
from python_proxy_headers.requests_adapter import ProxySession
with ProxySession(proxy_headers={'X-ProxyMesh-Country': 'US'}) as session:
session.proxies = {'https': 'http://user:pass@proxy.example.com:8080'}
response = session.get('https://httpbin.org/ip')
# Proxy headers are merged into response.headers
print(response.headers.get('X-ProxyMesh-IP'))
httpx
from python_proxy_headers.httpx_proxy import get
response = get(
'https://httpbin.org/ip',
proxy='http://user:pass@proxy.example.com:8080'
)
# Proxy CONNECT response headers are merged into response.headers
print(response.headers.get('X-ProxyMesh-IP'))
aiohttp
import asyncio
from python_proxy_headers.aiohttp_proxy import ProxyClientSession
async def main():
async with ProxyClientSession() as session:
async with session.get(
'https://httpbin.org/ip',
proxy='http://user:pass@proxy.example.com:8080'
) as response:
# Proxy headers merged into response.headers
print(response.headers.get('X-ProxyMesh-IP'))
asyncio.run(main())
pycurl (low-level)
import pycurl
from python_proxy_headers.pycurl_proxy import set_proxy_headers, HeaderCapture
c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://httpbin.org/ip')
c.setopt(pycurl.PROXY, 'http://proxy.example.com:8080')
# Add these two lines to any existing pycurl code
set_proxy_headers(c, {'X-ProxyMesh-Country': 'US'})
capture = HeaderCapture(c)
c.perform()
print(capture.proxy_headers) # Headers from proxy CONNECT response
c.close()
cloudscraper
from python_proxy_headers.cloudscraper_proxy import create_scraper
# Drop-in replacement for cloudscraper.create_scraper()
scraper = create_scraper(proxy_headers={'X-ProxyMesh-Country': 'US'})
scraper.proxies = {'https': 'http://proxy.example.com:8080'}
response = scraper.get('https://example.com')
# All CloudScraper features (Cloudflare bypass) preserved
Testing
A test harness is included to verify proxy header functionality:
# Set your proxy
export PROXY_URL='http://user:pass@proxy.example.com:8080'
# Test all modules
python test_proxy_headers.py
# Test specific modules
python test_proxy_headers.py requests httpx
# Verbose output (show header values)
python test_proxy_headers.py -v
Documentation
For detailed documentation, API reference, and more examples:
- Full Documentation: python-proxy-headers.readthedocs.io
- Example Code: proxy-examples for Python
Related Projects
- scrapy-proxy-headers - Proxy header support for Scrapy
About
Created by ProxyMesh to help our customers use custom headers to control proxy behavior. Works with any proxy that supports custom headers.
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_proxy_headers-0.2.2.tar.gz.
File metadata
- Download URL: python_proxy_headers-0.2.2.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6d756dc426120b7629f0753cc56efc7f48754fc762e9c6748d55cb7e540c6fb
|
|
| MD5 |
c79c0ae8c687e5322bee45db5ccfc312
|
|
| BLAKE2b-256 |
e2fc8ed30639c1e835f7cb887e3e0e118fdcf071318e0fa2e6d8109e5814ed98
|
Provenance
The following attestation bundles were made for python_proxy_headers-0.2.2.tar.gz:
Publisher:
publish.yml on proxymesh/python-proxy-headers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_proxy_headers-0.2.2.tar.gz -
Subject digest:
c6d756dc426120b7629f0753cc56efc7f48754fc762e9c6748d55cb7e540c6fb - Sigstore transparency entry: 1243763601
- Sigstore integration time:
-
Permalink:
proxymesh/python-proxy-headers@7e038335bb24b496ee8adeb1b78fad77b4d5e8dc -
Branch / Tag:
refs/heads/main - Owner: https://github.com/proxymesh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7e038335bb24b496ee8adeb1b78fad77b4d5e8dc -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file python_proxy_headers-0.2.2-py3-none-any.whl.
File metadata
- Download URL: python_proxy_headers-0.2.2-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3782b1ddb068fcfd4c307c85a99428d77f5b2d9178bda1e956b774740686f7a7
|
|
| MD5 |
f9742dda789e112b12bfcbc061a8e71b
|
|
| BLAKE2b-256 |
7f18992659d305dfa7f43965d52a47c3d986776334b9c68157f33e7690e11b39
|
Provenance
The following attestation bundles were made for python_proxy_headers-0.2.2-py3-none-any.whl:
Publisher:
publish.yml on proxymesh/python-proxy-headers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_proxy_headers-0.2.2-py3-none-any.whl -
Subject digest:
3782b1ddb068fcfd4c307c85a99428d77f5b2d9178bda1e956b774740686f7a7 - Sigstore transparency entry: 1243763603
- Sigstore integration time:
-
Permalink:
proxymesh/python-proxy-headers@7e038335bb24b496ee8adeb1b78fad77b4d5e8dc -
Branch / Tag:
refs/heads/main - Owner: https://github.com/proxymesh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7e038335bb24b496ee8adeb1b78fad77b4d5e8dc -
Trigger Event:
workflow_run
-
Statement type: