ó
ì2;Zc           @   sv  d  Z  d d l Z y d d l Z d d l Z Wn/ e k
 r_ d d l j Z d d l j Z n Xd d l	 Z	 d „  Z
 d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j e j f d	 „  ƒ  YZ e d
 k rrd d l Z y e e j d ƒ Z Wn e e f k
 rd Z n Xe j e e	 j d e ƒ ƒ Z d e j d ƒ j ƒ  j ƒ  GHd e j d ƒ j ƒ  j ƒ  GHn  d S(   sù   
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...
iÿÿÿÿNc         C   s   |  j  ƒ  } | j | ƒ | S(   N(   t   copyt   update(   t   at   bt   d(    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyt
   merge_dict   s    t   SocksiPyConnectionc           B   s&   e  Z d e d d d  „ Z d „  Z RS(   c   	      O   s5   | | | | | | f |  _  t j j |  | | Ž d  S(   N(   t	   proxyargst   httplibt   HTTPConnectiont   __init__(	   t   selft	   proxytypet	   proxyaddrt	   proxyportt   rdnst   usernamet   passwordt   argst   kwargs(    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR
      s    c         C   ss   t  j ƒ  |  _ |  j j |  j Œ  t |  j ƒ t t f k rS |  j j	 |  j ƒ n  |  j j
 |  j |  j f ƒ d  S(   N(   t   sockst
   socksockett   sockt   setproxyR   t   typet   timeoutt   intt   floatt
   settimeoutt   connectt   hostt   port(   R   (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR      s
    N(   t   __name__t
   __module__t   Nonet   TrueR
   R   (    (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR      s   t   SocksiPyConnectionSc           B   s&   e  Z d e d d d  „ Z d „  Z RS(   c   	      O   s5   | | | | | | f |  _  t j j |  | | Ž d  S(   N(   R   R   t   HTTPSConnectionR
   (	   R   R   R   R   R   R   R   R   R   (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR
   '   s    c         C   s…   t  j ƒ  } | j |  j Œ  t |  j ƒ t t f k rJ | j |  j ƒ n  | j	 |  j
 |  j f ƒ t j | |  j |  j ƒ |  _ d  S(   N(   R   R   R   R   R   R   R   R   R   R   R   R   t   sslt   wrap_sockett   key_filet	   cert_fileR   (   R   R   (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR   +   s    N(   R    R!   R"   R#   R
   R   (    (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR$   &   s   t   SocksiPyHandlerc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         O   s&   | |  _  | |  _ t j j |  ƒ d  S(   N(   R   t   kwt   urllib2t   HTTPHandlerR
   (   R   R   R   (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR
   4   s    		c            s%   d  d ‡  f d † } ˆ  j | | ƒ S(   Ni    c      	      s:   t  ˆ  j | ƒ } t d |  d | d | ˆ  j | Ž } | S(   NR   R   R   (   R   R+   R   R   (   R   R   R   R   R+   t   conn(   R   (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyt   build:   s    $(   R"   t   do_open(   R   t   reqR/   (    (   R   s0   /usr/lib/python2.7/dist-packages/sockshandler.pyt	   http_open9   s    c            s%   d  d ‡  f d † } ˆ  j | | ƒ S(   Ni    c      	      s:   t  ˆ  j | ƒ } t d |  d | d | ˆ  j | Ž } | S(   NR   R   R   (   R   R+   R$   R   (   R   R   R   R   R+   R.   (   R   (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR/   A   s    $(   R"   R0   (   R   R1   R/   (    (   R   s0   /usr/lib/python2.7/dist-packages/sockshandler.pyt
   https_open@   s    (   R    R!   R
   R2   R3   (    (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyR*   3   s   		t   __main__i   iZ#  t	   localhosts   HTTP: s   http://httpbin.org/ips   HTTPS: s   https://httpbin.org/ip(   t   __doc__R&   R,   R   t   ImportErrort   urllib.requestt   requestt   http.clientt   clientR   R   R	   R   R%   R$   R-   t   HTTPSHandlerR*   R    t   sysR   t   argvR   t
   ValueErrort
   IndexErrort   build_openert   PROXY_TYPE_SOCKS5t   openert   opent   readt   decode(    (    (    s0   /usr/lib/python2.7/dist-packages/sockshandler.pyt   <module>	   s*   	
