Coder Social home page Coder Social logo

seahoh / extproxy Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 35 KB

ExtProxy extend urllib2's ProxyHandler to support extra proxy types: HTTPS, SOCKS. It provides a consistent user experience like HTTP proxy for the users.

License: MIT License

Python 100.00%
urllib proxyhandler https socks

extproxy's Introduction

ExtProxy

license release status code size

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 version package format monthly downloads

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.

extproxy's People

Contributors

daniruiz avatar seahoh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ahmadhammad301

extproxy's Issues

Syntax error

Line 60 of extproxy/socket_inet_p.py:

    if ip_string[:1] = "[":
                     ^
SyntaxError: invalid syntax

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.