ExtProxy

ExtProxy extend urllib2's ProxyHandler to support extra proxy types: HTTPS, SOCKS. It provides a consistent user experience like HTTP proxy for the users.
This script is using a non-side-effects monkey patch, it did not applied to build-in module socket, just inject some codes into Request, ProxyHandler, HTTPConnection, SSLContext method's processing. Don't need to worry about the patching, you can using everything like before, or you can unpatch it at any time.
Installation
Install from

pip install ExtProxy
Or download and Install from source code
python setup.py install
Compatibility
- Python >= 2.7
- Require PySocks to support SOCKS proxy type
Usage
from urllib.request import urlopen, build_opener, ProxyHandler
import extproxy
proxy = "http://127.0.0.1:8080"
import ssl
proxy = "https://127.0.0.1:8443"
cafile = "cafile path"
set_https_proxy(proxy, check_hostname=False, cafile=cafile)
context_settings = {
"protocol": ssl.PROTOCOL_TLSv1_2,
"cert_reqs": ssl.CERT_REQUIRED,
"check_hostname": True,
"cafile": "cafile path",
"capath": "cafiles dir path",
"cadata": b"ca data"
"certfile": "certfile path",
"keyfile": "keyfile path",
}
context = ssl._create_unverified_context(**context_settings)
...
set_https_proxy(proxy, context=context)
proxy = "socks://127.0.0.1:1080"
import os
os.environ["HTTP_PROXY"] = proxy
os.environ["HTTPS_PROXY"] = proxy
print(urlopen("https://httpbin.org/ip").read().decode())
opener = build_opener(ProxyHandler({
"http": proxy,
"https": proxy
}))
print(opener.open("https://httpbin.org/ip").read().decode())
extproxy.restore_items()
License
ExtProxy is released under the MIT License.