Scrapy Proxy Headers
Send custom headers to proxies and receive proxy response headers in Scrapy.
The Problem
When making HTTPS requests through a proxy, headers on request.headers go through the encrypted tunnel and the proxy does not see them. Through Scrapy 2.19, Scrapy also does not send custom headers on the CONNECT request. Scrapy 2.20.0 does. Either way, Scrapy does not keep the proxy's CONNECT response headers, such as X-ProxyMesh-IP.
┌──────────┐ CONNECT ┌───────┐ Encrypted ┌────────────┐
│ Scrapy │ ───────────────► │ Proxy │ ════════════════► │ Target URL │
└──────────┘ (unencrypted) └───────┘ (tunnel) └────────────┘
│ │
Proxy headers request.headers
go HERE go here (encrypted)
This extension solves the problem by:
- Sending custom headers to the proxy during the CONNECT handshake
- Capturing response headers from the proxy's CONNECT response
- Making those headers available in your spider
Installation
pip install scrapy-proxy-headers
Quick Start
1. Configure the Download Handler
In your Scrapy settings.py:
DOWNLOAD_HANDLERS = {
"https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler"
}
Or in your spider's custom_settings:
class MySpider(scrapy.Spider):
custom_settings = {
"DOWNLOAD_HANDLERS": {
"https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler"
}
}
2. Send Proxy Headers
Use request.meta["proxy_headers"] to send headers to the proxy:
import scrapy
class MySpider(scrapy.Spider):
name = "example"
async def start(self):
yield scrapy.Request(
url="https://api.ipify.org?format=json",
meta={
"proxy": "http://user:pass@proxy.example.com:31280",
"proxy_headers": {"X-ProxyMesh-Country": "US"}
}
)
def parse(self, response):
# Proxy response headers are available in response.headers
proxy_ip = response.headers.get("X-ProxyMesh-IP")
self.logger.info(f"Proxy IP: {proxy_ip}")
Use async def start(). Scrapy 2.16 removed start_requests(), so a spider that only defines start_requests() sends no requests on every Scrapy version this package supports (2.17 and newer).
X-ProxyMesh-Country is only honored by some ProxyMesh proxies. See the ProxyMesh docs.
3. Receive Proxy Response Headers
Headers from the proxy's CONNECT response are merged into response.headers. Headers the origin already sent are left unchanged.
def parse(self, response):
# Access headers sent by the proxy
proxy_ip = response.headers.get(b"X-ProxyMesh-IP")
if proxy_ip:
print(f"Request made through IP: {proxy_ip.decode()}")
Scrapy 2.20.0+ / native proxy_headers
Scrapy 2.20.0 (released 2026-11-06) is the first release with native request.meta["proxy_headers"] support (#3329, #7888). It sends those headers to the proxy in request.meta["proxy"], including on the HTTPS CONNECT request. Scrapy 2.20.0 requires Python 3.11 or newer.
scrapy-proxy-headers 0.3.0 detects that support from Scrapy's features, not from the version string. On Scrapy 2.20.0 and newer, including master, PROXY_HEADERS_MODE is "native": Scrapy sends proxy_headers, and this package does not send them again. It only copies the proxy's CONNECT response headers, such as X-ProxyMesh-IP, onto response.headers. On Scrapy 2.17.0 through 2.19.x the mode is "legacy" and this package still writes proxy_headers on CONNECT itself.
Header-only users on Scrapy 2.20.0 and newer can drop the DOWNLOAD_HANDLERS override and keep the same meta value:
yield scrapy.Request(
url="https://api.ipify.org?format=json",
meta={
"proxy": "http://user:pass@proxy.example.com:31280",
"proxy_headers": {"X-ProxyMesh-Country": "US"},
},
)
Scrapy reads the CONNECT status line and discards the rest of the proxy response headers. X-ProxyMesh-IP and other CONNECT response headers are still only available through this package. Keep the download handler when the spider reads them from response.headers:
DOWNLOAD_HANDLERS = {
"https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler",
}
scrapy_proxy_headers.PROXY_HEADERS_MODE is "native" or "legacy". It is detected from the installed Scrapy. It is not a Scrapy setting. In native mode the captured CONNECT response is merged into the Scrapy response even when Scrapy reuses the tunnel.
import scrapy_proxy_headers
print(scrapy_proxy_headers.PROXY_HEADERS_MODE)
Complete Example
import scrapy
class ProxyHeadersSpider(scrapy.Spider):
name = "proxy_headers_demo"
custom_settings = {
"DOWNLOAD_HANDLERS": {
"https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler"
}
}
async def start(self):
yield scrapy.Request(
url="https://api.ipify.org?format=json",
meta={
"proxy": "http://user:pass@proxy.example.com:31280",
"proxy_headers": {"X-ProxyMesh-Country": "US"}
},
callback=self.parse_ip
)
def parse_ip(self, response):
data = response.json()
proxy_ip = response.headers.get(b"X-ProxyMesh-IP")
self.logger.info(f"Public IP: {data['ip']}")
if proxy_ip:
self.logger.info(f"Proxy IP: {proxy_ip.decode()}")
yield {
"public_ip": data["ip"],
"proxy_ip": proxy_ip.decode() if proxy_ip else None
}
How It Works
- HTTP11ProxyDownloadHandler - Custom download handler that manages proxy header caching
- Legacy path (
PROXY_HEADERS_MODE == "legacy") -ScrapyProxyHeadersAgentreadsproxy_headersandTunnelingHeadersAgentsends them on CONNECT - Native path (
PROXY_HEADERS_MODE == "native") - Scrapy sendsproxy_headers; the handler only records the CONNECT response
On the legacy path, TunnelingHeadersTCP4ClientEndpoint captures proxy response headers from the CONNECT response. On the native path, a subclass of Scrapy's tunnel protocol does that capture.
The handler caches proxy response headers by proxy URL and the normalized proxy_headers for that request. A reused tunnel still exposes the CONNECT response from the header set that opened it, and a different header set does not reuse another set's cached X-ProxyMesh-IP.
Test Harness
A test harness is included to verify proxy header functionality:
# Basic test
PROXY_URL=http://your-proxy:port TEST_URL=https://api.ipify.org python test_proxy_headers.py
# With custom proxy header
PROXY_URL=http://your-proxy:port \
PROXY_HEADER=X-ProxyMesh-IP \
SEND_PROXY_HEADER=X-ProxyMesh-Country \
SEND_PROXY_VALUE=US \
python test_proxy_headers.py
# Verbose output
python test_proxy_headers.py -v
Environment Variables
| Variable | Description | Default |
|---|---|---|
PROXY_URL |
Proxy URL (also checks HTTPS_PROXY) |
Required |
TEST_URL |
URL to request | https://api.ipify.org?format=json |
PROXY_HEADER |
Response header to check for | X-ProxyMesh-IP |
SEND_PROXY_HEADER |
Header name to send to proxy | Optional |
SEND_PROXY_VALUE |
Value for the send header | Optional |
Documentation
Full documentation is available at scrapy-proxy-headers.readthedocs.io.
Use Cases
- Geographic targeting: Send
X-ProxyMesh-Countryto route through specific countries - Session consistency: Request the same IP across multiple requests
- Debugging: Capture proxy response headers to see which IP was assigned
- Load balancing: Use proxy headers to control request distribution
Requirements
- Python 3.10+
- Scrapy 2.17.0+ (no upper cap). Native
proxy_headersstarts at Scrapy 2.20.0.
Scrapy 2.17.0 and 2.18 support Python 3.10 through 3.14. Scrapy 2.19 and 2.20 also list 3.15. Scrapy 2.20.0 and Scrapy master require Python 3.11+.
Local compatibility tests do not need a proxy account:
pip install -e ".[dev]"
pytest
License
BSD License - see LICENSE for details.
Links
Metadata
Release files for scrapy-proxy-headers 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| scrapy_proxy_headers-0.3.0.tar.gz | 15.5 kB | With provenance | Details for scrapy_proxy_headers-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| scrapy_proxy_headers-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for scrapy_proxy_headers-0.3.0-py3-none-any.whl |
Total release size: 28.2 kB
Release files / scrapy_proxy_headers-0.3.0.tar.gz
| Download URL | scrapy_proxy_headers-0.3.0.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a0acf2251b58556cb320362dc83aa1918edfd51457db681580e434e491681f5f
|
|
BLAKE2b-256 checksum How to use checksums |
de1308dfb5a7eb4d7e7bfc89a16acac5a39f694f1bc5d21090760d9f1999d8c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.
Transparency logRelease files / scrapy_proxy_headers-0.3.0-py3-none-any.whl
| Download URL | scrapy_proxy_headers-0.3.0-py3-none-any.whl |
|---|---|
| Size | 12.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
466ad02d568d9c139f29c86749583ee8263140526fee9ac73ec8cd8bf1cab814
|
|
BLAKE2b-256 checksum How to use checksums |
acc031bddf999d5694480481382547d0e9278d3de82fbe642c5afb1679f0c980
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.
Transparency logSecurity for scrapy-proxy-headers 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for scrapy_proxy_headers-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.