Skip to content

Commit 8317a32

Browse files
committed
initial import
0 parents  commit 8317a32

File tree

6 files changed

+465
-0
lines changed

6 files changed

+465
-0
lines changed

.gitignore

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# UV
98+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
#uv.lock
102+
103+
# poetry
104+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105+
# This is especially recommended for binary packages to ensure reproducibility, and is more
106+
# commonly ignored for libraries.
107+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108+
#poetry.lock
109+
110+
# pdm
111+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112+
#pdm.lock
113+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114+
# in version control.
115+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116+
.pdm.toml
117+
.pdm-python
118+
.pdm-build/
119+
120+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121+
__pypackages__/
122+
123+
# Celery stuff
124+
celerybeat-schedule
125+
celerybeat.pid
126+
127+
# SageMath parsed files
128+
*.sage.py
129+
130+
# Environments
131+
.env
132+
.venv
133+
env/
134+
venv/
135+
ENV/
136+
env.bak/
137+
venv.bak/
138+
139+
# Spyder project settings
140+
.spyderproject
141+
.spyproject
142+
143+
# Rope project settings
144+
.ropeproject
145+
146+
# mkdocs documentation
147+
/site
148+
149+
# mypy
150+
.mypy_cache/
151+
.dmypy.json
152+
dmypy.json
153+
154+
# Pyre type checker
155+
.pyre/
156+
157+
# pytype static type analyzer
158+
.pytype/
159+
160+
# Cython debug symbols
161+
cython_debug/
162+
163+
# PyCharm
164+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166+
# and can be added to the global gitignore or merged into this file. For a more nuclear
167+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
168+
#.idea/
169+
170+
# PyPI configuration file
171+
.pypirc

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 ProxyMesh
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# python-proxy-headers
2+
3+
## urllib3
4+
5+
ProxyManager inherits from PoolManager, __init__ accepts proxy_headers kwarg, puts into connection_pool_kw
6+
7+
PoolManager passes connection_pool_kw as kwargs into HTTPSConnectionPool (HTTPConnectionPool just adds them to normal headers)
8+
9+
//urlopen() creates conn through multiple calls ending up in _new_pool()
10+
11+
HTTPSConnectionPool __init__() receives _proxy_headers kwarg
12+
in _prepare_proxy(), passes self.proxy_headers to HTTPSConnection.set_tunnel() as headers
13+
_prepare_proxy() is called in HTTPConnectionPool.urlopen() with conn
14+
conn is created in _new_conn() using ConnectionCls
15+
_new_conn() is called by _get_conn()
16+
HTTPSConnectionPool sets ConnectionCls to HTTPSConnection
17+
18+
HTTPSConnection inherits from HTTPConnection
19+
HTTPConnection inherits from http.client.HTTPConnection
20+
HTTPConnection.set_tunnel() is simple wrapper around http.client.HTTPConnection.set_tunnel()
21+
HTTPConnection defines own _tunnel() method for python < 3.11.4
22+
_tunnel() is called by connect()
23+
24+
response headers are only available within _tunnel() method, set_tunnel() only sets the proxy headers
25+
26+
## http.client is python stdlib
27+
28+
HTTPConnection.set_tunnel() receives headers, stores for passing in CONNECT method in _tunnel()
29+
30+
in python3.12, _tunnel() reads proxy headers, saves them in _raw_proxy_headers
31+
can get _raw_proxy_headers using get_proxy_response_headers()
32+
33+
for older python, need to patch _tunnel() to get response headers
34+
35+
## TODO
36+
1. figure easiest urllib3 based method to pass in proxy_headers for requests
37+
``` python
38+
import urllib3
39+
proxy = urllib3.ProxyManager('http://de.proxymesh.com:31280', proxy_headers={'X-ProxyMesh-IP': '165.232.115.32'})
40+
r = proxy.request('GET', 'https://proxymesh.com/api/headers/')
41+
# NOTE that when using this method, even without proxy_headers, the proxymesh proxy might still keep the same IP
42+
# because urllib3 by default re-uses the connection
43+
```
44+
2. potentially create helper method(s) for doing this
45+
3. figure out how to patch or extend urllib3 ProxyManager to get proxy response headers in python3.12
46+
``` python
47+
from python_proxy_headers import connection
48+
proxy = connection.ProxyHeaderManager('http://de.proxymesh.com:31280', proxy_headers={'X-ProxyMesh-IP': '46.101.181.63'})
49+
r = proxy.request('GET', 'https://proxymesh.com/api/headers/')
50+
r.headers['X-ProxyMesh-IP']
51+
```
52+
4. figure out how to do create equivalent functionality for older pythons
53+
* tested with python3.7 & urllib3 1.26.20
54+
* tested with python3.12 & urllib3 2.3.0
55+
5. figure out how python requests uses urllib3 and easiest method for passing in proxy headers
56+
6. potentially create helper methods for doing this
57+
``` python
58+
from python_proxy_headers import adapter
59+
r = adapter.get('https://proxymesh.com/api/headers/', proxies={'http': 'http://de.proxymesh.com:31280', 'https': 'http://de.proxymesh.com:31280'}, proxy_headers={'x-proxymesh-ip': '46.101.236.88'})
60+
r.headers['X-ProxyMesh-IP']
61+
```
62+
7. pass proxy response headers from urllib3 functions back to requests response
63+
* tested on python3.7 & requests 2.31.0
64+
* tested with python3.12 & requests 2.32.3
65+
8. create adapters/extension for httpx library too

python_proxy_headers/__init__.py

Whitespace-only changes.

python_proxy_headers/adapter.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from requests.adapters import HTTPAdapter
2+
from requests.sessions import Session
3+
from .connection import proxy_from_url
4+
5+
class HTTPProxyHeaderAdapter(HTTPAdapter):
6+
def __init__(self, proxy_headers=None):
7+
super().__init__()
8+
self._proxy_headers = proxy_headers or {}
9+
10+
def proxy_manager_for(self, proxy, **proxy_kwargs):
11+
"""Return urllib3 ProxyManager for the given proxy.
12+
13+
This method should not be called from user code, and is only
14+
exposed for use when subclassing the
15+
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
16+
17+
:param proxy: The proxy to return a urllib3 ProxyManager for.
18+
:param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager.
19+
:returns: ProxyManager
20+
:rtype: urllib3.ProxyManager
21+
"""
22+
if proxy in self.proxy_manager:
23+
manager = self.proxy_manager[proxy]
24+
elif proxy.lower().startswith("socks"):
25+
return super().proxy_manager_for(proxy, **proxy_kwargs)
26+
else:
27+
# HTTPAdapter.proxy_headers only gets Proxy-Authorization
28+
_proxy_headers = self.proxy_headers(proxy)
29+
if self._proxy_headers:
30+
_proxy_headers.update(self._proxy_headers)
31+
32+
manager = self.proxy_manager[proxy] = proxy_from_url(
33+
proxy,
34+
proxy_headers=_proxy_headers,
35+
num_pools=self._pool_connections,
36+
maxsize=self._pool_maxsize,
37+
block=self._pool_block,
38+
**proxy_kwargs,
39+
)
40+
41+
return manager
42+
43+
class ProxySession(Session):
44+
def __init__(self, proxy_headers=None):
45+
super().__init__()
46+
self.mount('https://', HTTPProxyHeaderAdapter(proxy_headers=proxy_headers))
47+
self.mount('http://', HTTPProxyHeaderAdapter(proxy_headers=proxy_headers))
48+
49+
def request(method, url, proxy_headers=None, **kwargs):
50+
with ProxySession(proxy_headers) as session:
51+
return session.request(method=method, url=url, **kwargs)
52+
53+
def get(*args, **kwargs):
54+
return request('get', *args, **kwargs)
55+
56+
def options(*args, **kwargs):
57+
return request('options', *args, **kwargs)
58+
59+
def head(*args, **kwargs):
60+
kwargs.setdefault("allow_redirects", False)
61+
return request('head', *args, **kwargs)
62+
63+
def post(*args, **kwargs):
64+
return request('post', *args, **kwargs)
65+
66+
def put(*args, **kwargs):
67+
return request('put', *args, **kwargs)
68+
69+
def patch(*args, **kwargs):
70+
return request('patch', *args, **kwargs)
71+
72+
def delete(*args, **kwargs):
73+
return request('delete', *args, **kwargs)

0 commit comments

Comments
 (0)