U "@Wa @s&dZddlZzddlZddlZWn,ek rLddlmZddlmZYnXddl Z ddZ Gdddej Z Gdddej ZGd d d ejejZed kr"ddlZzeejd ZWneefk rd ZYnXeee jdeZededededdS)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|}|||SN)copyupdate)abdr/sockshandler.py merge_dicts r c@seZdZdddZddZdS)SocksiPyConnectionNTc Os*||||||f|_tjj|f||dSr) proxyargshttplibHTTPConnection__init__ selfZ proxytypeZ proxyaddrZ proxyportZrdnsZusernameZpasswordargskwargsrrr rszSocksiPyConnection.__init__cCsPt|_|jj|jt|jttfkr8|j |j|j |j |j fdSr) socks socksocketsocksetproxyr typetimeoutintfloat settimeoutconnecthostportrrrr rs  zSocksiPyConnection.connect)NTNN__name__ __module__ __qualname__rrrrrr r s r c@seZdZdddZddZdS)SocksiPyConnectionSNTc Os*||||||f|_tjj|f||dSr)r r HTTPSConnectionrrrrr r'szSocksiPyConnectionS.__init__cCs\t}|j|jt|jttfkr2||j| |j |j ft ||j|j|_dSr)rrrr rrrrrrrrsslZ wrap_socketZkey_fileZ cert_filer)rrrrr r+s   zSocksiPyConnectionS.connect)NTNNr!rrrr r%&s r%c@s$eZdZddZddZddZdS)SocksiPyHandlercOs||_||_tj|dSr)rkwurllib2 HTTPHandlerr)rrrrrr r4szSocksiPyHandler.__init__csdfdd }||S)Nrcs(tj|}tj|||d|}|SN)rrr)r r)r rrrrrr)Zconnr rr build:s z(SocksiPyHandler.http_open..build)NrZdo_openrZreqr.rr r http_open9szSocksiPyHandler.http_opencsdfdd }||S)Nrcs(tj|}tj|||d|}|Sr,)r r)r%rr-r rr r.As z)SocksiPyHandler.https_open..build)Nrr/r0rr r https_open@szSocksiPyHandler.https_openN)r"r#r$rr1r2rrrr r(3sr(__main__iZ#Z localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip) __doc__r'r*r ImportErrorZurllib.requestZrequestZ http.clientZclientrr rr r&r%r+Z HTTPSHandlerr(r"sysrargvr ValueError IndexErrorZ build_openerZPROXY_TYPE_SOCKS5Zopenerprintopenreaddecoderrrr s*