
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
ì2;Za  ã               @   s"  d Z ddlZyddlZddlZW n( ek
rH   ddljZddljZY nX ddl	Z	dd„ Z
G dd„ dejƒZG dd„ dejƒZG d	d
„ d
ejejƒZedk�rddlZyeejd ƒZW n eefk
rÔ   dZY nX ejee	jdeƒƒZedejdƒjƒ jƒ  ƒ edejdƒjƒ jƒ  ƒ dS )zù
SocksiPy + urllib2 handler

version: 0.3
author: e<e@tr0ll.in>

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...
é    Nc             C   s   | j ƒ }|j|ƒ |S )N)ÚcopyÚupdate)ÚaÚbÚd© r   ú"/usr/lib/python3.6/sockshandler.pyÚ
merge_dict   s    
r	   c               @   s   e Zd Zddd„Zdd„ ZdS )ÚSocksiPyConnectionNTc       	      O   s*   ||||||f| _ tjj| f|ž|Ž d S )N)Ú	proxyargsÚhttplibÚHTTPConnectionÚ__init__)	ÚselfÚ	proxytypeÚ	proxyaddrÚ	proxyportÚrdnsÚusernameÚpasswordÚargsÚkwargsr   r   r   r      s    zSocksiPyConnection.__init__c             C   sP   t jƒ | _| jj| jŽ  t| jƒttfkr8| jj	| jƒ | jj
| j| jfƒ d S )N)ÚsocksÚ
socksocketÚsockÚsetproxyr   ÚtypeÚtimeoutÚintÚfloatÚ
settimeoutÚconnectÚhostÚport)r   r   r   r   r!      s
    
zSocksiPyConnection.connect)NTNN)Ú__name__Ú
__module__Ú__qualname__r   r!   r   r   r   r   r
      s   
r
   c               @   s   e Zd Zddd„Zdd„ ZdS )ÚSocksiPyConnectionSNTc       	      O   s*   ||||||f| _ tjj| f|ž|Ž d S )N)r   r   ÚHTTPSConnectionr   )	r   r   r   r   r   r   r   r   r   r   r   r   r   '   s    zSocksiPyConnectionS.__init__c             C   s\   t jƒ }|j| jŽ  t| jƒttfkr2|j| jƒ |j	| j
| jfƒ tj|| j| jƒ| _d S )N)r   r   r   r   r   r   r   r   r    r!   r"   r#   ÚsslZwrap_socketZkey_fileZ	cert_filer   )r   r   r   r   r   r!   +   s    zSocksiPyConnectionS.connect)NTNN)r$   r%   r&   r   r!   r   r   r   r   r'   &   s   
r'   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSocksiPyHandlerc             O   s   || _ || _tjj| ƒ d S )N)r   ÚkwÚurllib2ÚHTTPHandlerr   )r   r   r   r   r   r   r   4   s    zSocksiPyHandler.__init__c                s   d‡ fdd„	}ˆ j ||ƒS )Nr   c                s(   t ˆ j|ƒ}tˆ j| ||dœ|—Ž}|S )N)r"   r#   r   )r	   r+   r
   r   )r"   r#   r   r   r+   Úconn)r   r   r   Úbuild:   s    z(SocksiPyHandler.http_open.<locals>.build)Nr   )Údo_open)r   Úreqr/   r   )r   r   Ú	http_open9   s    zSocksiPyHandler.http_openc                s   d‡ fdd„	}ˆ j ||ƒS )Nr   c                s(   t ˆ j|ƒ}tˆ j| ||dœ|—Ž}|S )N)r"   r#   r   )r	   r+   r'   r   )r"   r#   r   r   r+   r.   )r   r   r   r/   A   s    z)SocksiPyHandler.https_open.<locals>.build)Nr   )r0   )r   r1   r/   r   )r   r   Ú
https_open@   s    zSocksiPyHandler.https_openN)r$   r%   r&   r   r2   r3   r   r   r   r   r*   3   s   r*   Ú__main__é   iZ#  Z	localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip) Ú__doc__r)   r,   r   ÚImportErrorZurllib.requestZrequestZhttp.clientZclientr   r	   r   r
   r(   r'   r-   ZHTTPSHandlerr*   r$   Úsysr   Úargvr#   Ú
ValueErrorÚ
IndexErrorZbuild_openerZPROXY_TYPE_SOCKS5ÚopenerÚprintÚopenÚreadÚdecoder   r   r   r   Ú<module>	   s*   


