scrapy-proxy-headers 0.2.1 fixes the package on Scrapy 2.16 and later, where version 0.2.0 fails to load. It also raises the minimum Scrapy version to 2.14.2, the release that patches a high-severity Referrer-Policy vulnerability. If you run Scrapy 2.16 or newer, or still have Scrapy 2.14.1 installed, upgrade now. Your settings and spider code don't need to change.

pip install -U scrapy-proxy-headers

Why 0.2.0 broke on Scrapy 2.16

Scrapy 2.16 made several classes in its HTTP/1.1 download handler private, including TunnelingAgent and ScrapyAgent. scrapy-proxy-headers 0.2.0 imported them by their old public names, so on Scrapy 2.16 through 2.18 Scrapy can't load HTTP11ProxyDownloadHandler. Every HTTPS request then fails with an error like this:

NotSupported: Unsupported URL scheme 'https': cannot import name 'TunnelingAgent'

Version 0.2.1 looks up both the old and the new names, so one release works on every Scrapy version from 2.14.2 to 2.18. It also builds its own tunneling agent for HTTPS requests sent through a proxy, so your proxy_headers still go out with the CONNECT request on newer Scrapy releases.

Security: Scrapy 2.14.2 is now required

GHSA-cwxj-rr6w-m6w7 is a high-severity advisory for Scrapy 1.4.0 through 2.14.1. When a site sent a Referrer-Policy header that looked like a Python import path, Scrapy imported that object and called it. A malicious site could use this to make your crawler call something like sys.exit and stop. Scrapy 2.14.2 fixes it.

scrapy-proxy-headers 0.2.0 still allowed Scrapy 2.14.1. Version 0.2.1 requires scrapy>=2.14.2, so pip install -U scrapy-proxy-headers also upgrades Scrapy if you're on the affected release.

Example: choose a country and read the proxy IP

Headers you set on a Scrapy request travel inside the encrypted HTTPS tunnel, so the proxy never sees them. scrapy-proxy-headers sends proxy_headers in the CONNECT request instead, and copies the proxy's reply headers into response.headers. With ProxyMesh, pick a country with X-ProxyMesh-Country and log which IP served each request from X-ProxyMesh-IP.

import scrapy


class CountrySpider(scrapy.Spider):
    name = "country"
    custom_settings = {
        "DOWNLOAD_HANDLERS": {
            "https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler",
        },
    }

    async def start(self):
        yield scrapy.Request(
            "https://api.ipify.org?format=json",
            meta={
                # X-ProxyMesh-Country works on the World and Open proxies.
                "proxy": "http://USERNAME:PASSWORD@PROXY_HOST:PORT",
                "proxy_headers": {"X-ProxyMesh-Country": "DE"},
            },
        )

    def parse(self, response):
        proxy_ip = response.headers.get(b"X-ProxyMesh-IP")
        yield {"proxy_ip": proxy_ip.decode() if proxy_ip else None}

See Proxy Server Request & Response Headers for every header ProxyMesh accepts and returns.

Other changes

  • An https:// proxy URL with an HTTPS destination now raises NotImplementedError, the same limit Scrapy 2.16 added to its own handler. ProxyMesh proxy URLs use http://, so this doesn't affect ProxyMesh setups.
  • HTTP11ProxyDownloadHandler.download_request() is now a coroutine, matching Scrapy's current download handler API. This only matters if you subclass the handler.

Install it from scrapy-proxy-headers 0.2.1 on PyPI. The GitHub release notes and the full diff from 0.2.0 list the changes. Usage is covered in the scrapy-proxy-headers documentation.