3 2;Za @s"dZddlZyddlZddlZWn(ek rHddljZddljZYnXddl Z ddZ Gdddej Z Gdddej ZGd d d ejejZed krddlZyeejd ZWneefk rd ZYnXejee jdeZedejdjjedejdjjdS)z SocksiPy + urllib2 handler version: 0.3 author: e This module provides a Handler which you can use with urllib2 to allow it to tunnel your connection through a socks.sockssocket socket, with out monkey patching the original socket... NcCs|j}|j||S)N)copyupdate)abdr"/usr/lib/python3.6/sockshandler.py merge_dicts r c@seZdZdddZddZdS)SocksiPyConnectionNTc Os*||||||f|_tjj|f||dS)N) proxyargshttplibHTTPConnection__init__) self proxytype proxyaddr proxyportrdnsusernamepasswordargskwargsrrrrszSocksiPyConnection.__init__cCsPtj|_|jj|jt|jttfkr8|jj |j|jj |j |j fdS)N) socks socksocketsocksetproxyr typetimeoutintfloat settimeoutconnecthostport)rrrrr!s  zSocksiPyConnection.connect)NTNN)__name__ __module__ __qualname__rr!rrrrr s r c@seZdZdddZddZdS)SocksiPyConnectionSNTc Os*||||||f|_tjj|f||dS)N)r r HTTPSConnectionr) rrrrrrrrrrrrr'szSocksiPyConnectionS.__init__cCs\tj}|j|jt|jttfkr2|j|j|j |j |j ft j ||j|j|_dS)N)rrrr rrrrr r!r"r#sslZ wrap_socketZkey_fileZ cert_filer)rrrrrr!+s   zSocksiPyConnectionS.connect)NTNN)r$r%r&rr!rrrrr'&s r'c@s$eZdZddZddZddZdS)SocksiPyHandlercOs||_||_tjj|dS)N)rkwurllib2 HTTPHandlerr)rrrrrrr4szSocksiPyHandler.__init__csdfdd }j||S)Nrcs(tj|}tj|||d|}|S)N)r"r#r)r r+r r)r"r#rrr+conn)rrrbuild:s z(SocksiPyHandler.http_open..build)Nr)do_open)rreqr/r)rr http_open9szSocksiPyHandler.http_opencsdfdd }j||S)Nrcs(tj|}tj|||d|}|S)N)r"r#r)r r+r'r)r"r#rrr+r.)rrrr/As z)SocksiPyHandler.https_open..build)Nr)r0)rr1r/r)rr https_open@szSocksiPyHandler.https_openN)r$r%r&rr2r3rrrrr*3sr*__main__iZ#Z localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip) __doc__r)r,r ImportErrorZurllib.requestZrequestZ http.clientZclientrr r r r(r'r-Z HTTPSHandlerr*r$sysrargvr# ValueError IndexErrorZ build_openerZPROXY_TYPE_SOCKS5openerprintopenreaddecoderrrr s*