|
| 1 | +--- |
| 2 | +name: scrapy-proxy-headers |
| 3 | +description: >- |
| 4 | + Send and receive custom headers during HTTPS CONNECT tunneling in Scrapy. |
| 5 | + Use when adding proxy headers to Scrapy spiders, configuring download handlers |
| 6 | + for proxy header support, or reading proxy response headers like X-ProxyMesh-IP. |
| 7 | +--- |
| 8 | + |
| 9 | +# scrapy-proxy-headers |
| 10 | + |
| 11 | +Send custom headers to proxies and receive proxy response headers in Scrapy. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install scrapy-proxy-headers |
| 17 | +``` |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +Add the download handler in `settings.py`: |
| 22 | + |
| 23 | +```python |
| 24 | +DOWNLOAD_HANDLERS = { |
| 25 | + "https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler" |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Or in spider's `custom_settings`: |
| 30 | + |
| 31 | +```python |
| 32 | +class MySpider(scrapy.Spider): |
| 33 | + custom_settings = { |
| 34 | + "DOWNLOAD_HANDLERS": { |
| 35 | + "https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler" |
| 36 | + } |
| 37 | + } |
| 38 | +``` |
| 39 | + |
| 40 | +## Sending Proxy Headers |
| 41 | + |
| 42 | +Use `request.meta["proxy_headers"]`: |
| 43 | + |
| 44 | +```python |
| 45 | +import scrapy |
| 46 | + |
| 47 | +class MySpider(scrapy.Spider): |
| 48 | + name = "example" |
| 49 | + |
| 50 | + def start_requests(self): |
| 51 | + yield scrapy.Request( |
| 52 | + url="https://api.ipify.org?format=json", |
| 53 | + meta={ |
| 54 | + "proxy": "http://your-proxy:port", |
| 55 | + "proxy_headers": {"X-ProxyMesh-Country": "US"} |
| 56 | + } |
| 57 | + ) |
| 58 | + |
| 59 | + def parse(self, response): |
| 60 | + proxy_ip = response.headers.get("X-ProxyMesh-IP") |
| 61 | + self.logger.info(f"Proxy IP: {proxy_ip}") |
| 62 | +``` |
| 63 | + |
| 64 | +## Receiving Proxy Response Headers |
| 65 | + |
| 66 | +Headers from the proxy's CONNECT response are merged into `response.headers`: |
| 67 | + |
| 68 | +```python |
| 69 | +def parse(self, response): |
| 70 | + proxy_ip = response.headers.get(b"X-ProxyMesh-IP") |
| 71 | + if proxy_ip: |
| 72 | + print(f"Request made through IP: {proxy_ip.decode()}") |
| 73 | +``` |
| 74 | + |
| 75 | +Note: Headers are bytes in Scrapy; decode with `.decode()`. |
| 76 | + |
| 77 | +## Complete Example |
| 78 | + |
| 79 | +```python |
| 80 | +import scrapy |
| 81 | + |
| 82 | +class ProxyHeadersSpider(scrapy.Spider): |
| 83 | + name = "proxy_headers_demo" |
| 84 | + |
| 85 | + custom_settings = { |
| 86 | + "DOWNLOAD_HANDLERS": { |
| 87 | + "https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler" |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + def start_requests(self): |
| 92 | + yield scrapy.Request( |
| 93 | + url="https://api.ipify.org?format=json", |
| 94 | + meta={ |
| 95 | + "proxy": "http://us.proxymesh.com:31280", |
| 96 | + "proxy_headers": {"X-ProxyMesh-Country": "US"} |
| 97 | + }, |
| 98 | + callback=self.parse_ip |
| 99 | + ) |
| 100 | + |
| 101 | + def parse_ip(self, response): |
| 102 | + data = response.json() |
| 103 | + proxy_ip = response.headers.get(b"X-ProxyMesh-IP") |
| 104 | + |
| 105 | + yield { |
| 106 | + "public_ip": data["ip"], |
| 107 | + "proxy_ip": proxy_ip.decode() if proxy_ip else None |
| 108 | + } |
| 109 | +``` |
| 110 | + |
| 111 | +## Proxy Headers |
| 112 | + |
| 113 | +Custom headers sent during CONNECT are proxy-specific. Check your proxy provider's docs. |
| 114 | + |
| 115 | +Example with [ProxyMesh](https://proxymesh.com): |
| 116 | + |
| 117 | +| Header | Direction | Purpose | |
| 118 | +|--------|-----------|---------| |
| 119 | +| `X-ProxyMesh-Country` | Send | Route through specific country | |
| 120 | +| `X-ProxyMesh-IP` | Send/Receive | Request or receive sticky IP | |
| 121 | + |
| 122 | +## Testing |
| 123 | + |
| 124 | +```bash |
| 125 | +PROXY_URL=http://your-proxy:port python test_proxy_headers.py -v |
| 126 | +``` |
| 127 | + |
| 128 | +## Documentation |
| 129 | + |
| 130 | +Full docs at [scrapy-proxy-headers.readthedocs.io](https://scrapy-proxy-headers.readthedocs.io/). |
0 commit comments