Skip to main content

ExtProxy extend urllib2's ProxyHandler to support extra proxy types: HTTPS, SOCKS.

Project description

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. No need to worry about the patching, you can using everything like before, or you can unpatch it at any time.

Installation

Install from PyPI:

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

# Target can be imported before monkey patching
from urllib.request import urlopen, build_opener, ProxyHandler


# Import extproxy, auto apply monkey patching by `extproxy.patch_items`
import extproxy


# Use origin HTTP proxy
proxy = "http://127.0.0.1:8080"


# Use HTTPS proxy, use `set_https_proxy` to custom proxy's SSL verify mode
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"             # Uesd to server auth
    "certfile": "certfile path",  #
    "keyfile": "keyfile path",    # Uesd to client auth
}
context = ssl._create_unverified_context(**context_settings)
 ...  # More custom settings
set_https_proxy(proxy, context=context)


# Use SOCKS proxy, `socks` can be: socks, socks4, socks4a, socks5, socks5h
# SOCKS4 does not support remote resolving, but SOCKS4a/5 supported
# 'socks' means SOCKS5, 'socks5h' means do not use remote resolving
proxy = "socks://127.0.0.1:1080"


# Set proxy via system/python environment variables
import os
os.environ["HTTP_PROXY"] = proxy
os.environ["HTTPS_PROXY"] = proxy
print(urlopen("https://httpbin.org/ip").read().decode())


# Set proxy via ProxyHandler
opener = build_opener(ProxyHandler({
    "http": proxy,
    "https": proxy
}))
print(opener.open("https://httpbin.org/ip").read().decode())


# Restore monkey patch, then HTTPS, SOCKS proxy use can not continue working
extproxy.restore_items()

License

ExtProxy is released under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ExtProxy-0.1.3.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ExtProxy-0.1.3-py2.py3-none-any.whl (11.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ExtProxy-0.1.3.tar.gz.

File metadata

  • Download URL: ExtProxy-0.1.3.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.1

File hashes

Hashes for ExtProxy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 474e93af8c0219fc33c9c5d0af2a7a055046862f261670893d959baf787bfec1
MD5 b41d5fb0b0c7f2c956a3449269fbbf62
BLAKE2b-256 4d10beb3a9f53e6e2173a4536690e1f4fc60414e089149e3587ee5ca994772e0

See more details on using hashes here.

File details

Details for the file ExtProxy-0.1.3-py2.py3-none-any.whl.

File metadata

  • Download URL: ExtProxy-0.1.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.1

File hashes

Hashes for ExtProxy-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c5332e8dd2ade2d4e4c100320f84c52539c0c1eb2b7af2b1d320ec5c6d0e3b33
MD5 a46240ad6d1dcf801f1292cea385671d
BLAKE2b-256 a245ec6f904555afbaf1c1e9f8605e58a9ea902106d5e11f3a067c68b13d8df4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page