diff --git a/.agents/skills/python-proxy-headers/SKILL.md b/.agents/skills/python-proxy-headers/SKILL.md index 588131e..dbfb2d2 100644 --- a/.agents/skills/python-proxy-headers/SKILL.md +++ b/.agents/skills/python-proxy-headers/SKILL.md @@ -45,6 +45,8 @@ with ProxySession(proxy_headers={'X-ProxyMesh-Country': 'US'}) as session: ### httpx +Requires `httpx>=0.28,<1` (`pip install 'python-proxy-headers[httpx]'`). Importing `httpx_proxy` on httpx 1.x raises `ImportError`. + ```python from python_proxy_headers.httpx_proxy import get diff --git a/.github/workflows/proxy_integration_tests.yml b/.github/workflows/proxy_integration_tests.yml index 9f8f78b..9c47638 100644 --- a/.github/workflows/proxy_integration_tests.yml +++ b/.github/workflows/proxy_integration_tests.yml @@ -33,8 +33,8 @@ jobs: pip install -e . pip install urllib3 requests aiohttp httpx pycurl - - name: Run header security unit tests - run: python test_header_security.py + - name: Run header security and httpx guard tests + run: python test_header_security.py && python test_httpx_guard.py integration: runs-on: ubuntu-24.04 diff --git a/CHANGELOG.md b/CHANGELOG.md index 35fc0c4..94401c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Importing `python_proxy_headers.httpx_proxy` on httpx >= 1 raises `ImportError`. httpx 1.0 removed proxy support. The `[httpx]` extra is `httpx>=0.28,<1`. + ## 0.2.4 - 2026-11-09 ### Security diff --git a/docs/httpx.rst b/docs/httpx.rst index e33076d..dfa7e60 100644 --- a/docs/httpx.rst +++ b/docs/httpx.rst @@ -3,6 +3,11 @@ httpx `HTTPX `_ is a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2. This page describes how to use httpx with proxies and how to send and receive custom proxy headers. +httpx 1.0 +--------- + +httpx 1.0 removes httpcore and proxy support. Importing ``python_proxy_headers.httpx_proxy`` raises ``ImportError``. Install the supported line with ``pip install 'python-proxy-headers[httpx]'`` (``httpx>=0.28,<1``). + Getting Started --------------- @@ -14,7 +19,7 @@ This section shows you how to quickly get up and running with proxy headers in h .. code-block:: bash - pip install python-proxy-headers httpx + pip install 'python-proxy-headers[httpx]' 2. Import the module: diff --git a/pyproject.toml b/pyproject.toml index 29b4f43..f01e15c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,9 @@ classifiers = [ "Topic :: Software Development :: Libraries :: Python Modules", ] +[project.optional-dependencies] +httpx = ["httpx>=0.28,<1"] + [project.urls] Homepage = "https://github.com/proxymesh/python-proxy-headers" Changelog = "https://github.com/proxymesh/python-proxy-headers/commits/main/" diff --git a/python_proxy_headers/httpx_proxy.py b/python_proxy_headers/httpx_proxy.py index 430b5e3..bb26310 100644 --- a/python_proxy_headers/httpx_proxy.py +++ b/python_proxy_headers/httpx_proxy.py @@ -1,3 +1,11 @@ +import httpx + +if not httpx.__version__.startswith("0."): + raise ImportError( + f"python_proxy_headers.httpx_proxy requires httpx>=0.28,<1 (found {httpx.__version__}); " + "httpx 1.0 removed proxy support. Install with: pip install 'python-proxy-headers[httpx]'" + ) + from contextlib import contextmanager from httpcore._sync.http_proxy import HTTPProxy, TunnelHTTPConnection, merge_headers, logger from httpcore._sync.http11 import HTTP11Connection @@ -386,4 +394,4 @@ def stream(method: str, mounts={'http://': transport, 'https://': transport} ) as client: with client.stream(method=method, url=url, **kwargs) as response: - yield response \ No newline at end of file + yield response diff --git a/test_httpx_guard.py b/test_httpx_guard.py new file mode 100644 index 0000000..8e1872d --- /dev/null +++ b/test_httpx_guard.py @@ -0,0 +1,42 @@ +"""httpx >= 1 must raise ImportError when the httpx adapter is imported.""" + +import os +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parent + + +class HttpxGuardTests(unittest.TestCase): + def test_httpx_1_dev6_raises_import_error(self) -> None: + fake = Path(tempfile.mkdtemp()) + package = fake / "httpx" + package.mkdir() + (package / "__init__.py").write_text( + '__version__ = "1.0.dev6"\n', encoding="utf-8" + ) + env = os.environ.copy() + prior = env.get("PYTHONPATH") + parts = [str(fake), str(ROOT)] + if prior: + parts.append(prior) + env["PYTHONPATH"] = os.pathsep.join(parts) + completed = subprocess.run( + [sys.executable, "-c", "import python_proxy_headers.httpx_proxy"], + env=env, + text=True, + capture_output=True, + timeout=30, + check=False, + ) + self.assertNotEqual(completed.returncode, 0) + self.assertIn("ImportError", completed.stderr) + self.assertIn("1.0.dev6", completed.stderr) + self.assertIn("python-proxy-headers[httpx]", completed.stderr) + + +if __name__ == "__main__": + unittest.main()