;ò
z¨+:c               si   d  Z  d Z d Z d k Z d k Z d k Z d k Z d Z d e f d „  ƒ  YZ	 d f  d „  ƒ  YZ
 d S(	   sÆ   Sockets with timeouts.

Defines a "timeout_socket" class for connections that can potentially
cause the server to hang.  It uses select, and is based on code originally
from Scott Cotton.

The following people have contributed to the development of this module:

        Scott Cotton <scott@chronis.pobox.com>
        Lloyd Zusman <ljz@asfast.com
        Phil Mayes <pmayes@olivebr.com>
        Piers Lauder <piers@cs.su.oz.au>

Here are examples using smtplib.py and poplib.py by inheriting:

class timeout_POP3(poplib.POP3):
    def __init__(self, host, port = poplib.POP3_PORT):
        self.host = host
        self.port = port
        self.sock = timeout_socket.timeout_socket()
        self.sock.connect(self.host, self.port)
        self.file = self.sock.makefile('rb') # same as poplib.POP3
        self._debugging = 0
        self.welcome = self._getresp()

class timeout_SMTP(smtplib.SMTP):
    def connect(self, host='localhost', port = 0):
        '''Connect to a host on a given port.
        Override the std SMTP connect in order to use a timeout_socket.
        '''
        if not port:
            i = string.find(host, ':')
            if i >= 0:
                host, port = host[:i], host[i+1:]
                try: port = string.atoi(port)
                except string.atoi_error:
                    raise socket.error, "nonnumeric port"
        if not port: port = smtplib.SMTP_PORT
        self.sock = timeout_socket.timeout_socket()
        if self.debuglevel > 0: print 'connect:', (host, port)
        self.sock.connect(host, port)
        (code,msg)=self.getreply()
        if self.debuglevel >0 : print "connect:", msg
        return (code,msg)

This allows one to use poplib.py & smtplib.py unchanged.
s   1.7s&   Scott Cotton <scott@chronis.pobox.com>Nf20.0s   Timeoutc              s   t  Z RS(   N(   s   __name__s
   __module__(    (    (    s   ./timeout_socket.pys   Timeout;   s   s   timeout_socketc              s¡   t  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d d „ Z	 d d „ Z
 d d	 d
 „ Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z RS(   sÞ    Instantiate with:

                timeout_socket(timeout=20, sock=None)

        where `timeout' is the desired timeout in seconds (default 20),
        and `sock' is a pre-existing socket (default: one is created).
    c            sn   d |  _ |  i | ƒ d |  _ | t j o t i t i t i ƒ } n | |  _	 d |  _ |  i	 i
 d ƒ d  S(   Ni    s    i   (   s   selfs   ndupss   timeouts   inbufs   socks   Nones   sockets   AF_INETs   SOCK_STREAMs   ss   setblocking(   s   selfs   timeouts   sock(    (    s   ./timeout_socket.pys   __init__H   s    				c            s   t  |  i | ƒ Sd  S(   N(   s   getattrs   selfs   ss   name(   s   selfs   name(    (    s   ./timeout_socket.pys   __getattr__T   s    c   
         ss  |  i } |  i } y4 | i d ƒ | i ƒ  } | i | d j ƒ | SWnt i j
 oþ }	 | o ‚  n | i d ƒ t
 |	 i ƒ d j o
 d } n |	 \ } }	 | t i t i f j o ‚  n t i | g g  g  | ƒ \ } } } | ob y | i ƒ  } | SWnG t i j
 o8 }	 t
 |	 i ƒ d j o
 d } n |	 \ } }	 ‚  n Xn n Xd |  i } t | ƒ ‚ d  S(   Ni   i    s!   accept timed out after %s seconds(   s   selfs   _timeouts   timeouts   ss   setblockings   accepts   sas   sockets   errors   whys   lens   argss   codes   errnos   EAGAINs   EWOULDBLOCKs   selects   rs   ws   es   msgs   Timeout(
   s   selfs   codes   es   timeouts   ss   rs   ws   msgs   sas   why(    (    s   ./timeout_socket.pys   acceptW   s8    		
$
c   
         sŸ  |  i } |  i } y8 | i d ƒ t | i | ƒ | i | d j ƒ d SWn/t i	 j
 o }	 | o ‚  n | i d ƒ t |	 i ƒ d j o
 d } n |	 \ } }	 | t i t i t i f j o ‚  n t i g  | g g  | ƒ \ } } } | o~ y t | i | ƒ d SWn_ t i	 j
 oP }	 t |	 i ƒ d j o
 d } n |	 \ } }	 | t i j o d Sn ‚  n Xn n Xd t |  i f } t | ƒ ‚ d  S(   Ni    i   s(   connect to %s timed out after %s seconds(   s   selfs   _timeouts   timeouts   ss   setblockings   applys   connects   addrs   sockets   errors   whys   lens   argss   codes   errnos   EINPROGRESSs   EALREADYs   EWOULDBLOCKs   selects   rs   ws   es   EISCONNs   hosts   msgs   Timeout(
   s   selfs   addrs   codes   es   ws   ss   rs   timeouts   msgs   why(    (    s   ./timeout_socket.pys   connect|   s<    		
$
i    c            s»   d } |  i } t | ƒ }
 x™ d o‘ t i g  |  i g g  | ƒ \ } }	 } |	 oI | | | d !} |  i i | | ƒ } | | } | |
 j o d  Sn n t d | | f ƒ ‚ q Wd  S(   Ni    i   i    s(   timeout while sending "%.20s...": %d sec(   s   nexts   selfs   _timeouts   ts   lens   datas   totals   selects   ss   rs   ws   es   bufs   sends   flagss   sents   Timeout(   s   selfs   datas   flagss   es   bufs   sents   nexts   rs   ts   ws   total(    (    s   ./timeout_socket.pys   send£   s    	 '
c            su   t  i  |  i g g  g  |  i ƒ \ } } } | o |  i i | | ƒ } | Sn t d |  i i ƒ  |  i f ƒ ‚ d  S(   Ns'   timeout while receiving from %s: %d sec(   s   selects   selfs   ss   _timeouts   rs   ws   es   recvs   amts   flagss   recvds   Timeouts   getpeername(   s   selfs   amts   flagss   rs   es   ws   recvd(    (    s   ./timeout_socket.pys   recv²   s
    *s   riÿÿÿÿc            s   |  i d |  _ |  Sd  S(   Ni   (   s   selfs   ndups(   s   selfs   flagss   buffsize(    (    s   ./timeout_socket.pys   makefile¹   s    c            s5   |  i d |  _ |  i d j o |  i i ƒ  n d  S(   Ni   i    (   s   selfs   ndupss   ss   close(   s   self(    (    s   ./timeout_socket.pys   closeÁ   s    c            s\   x4 t  |  i ƒ | j  o |  i |  i d ƒ |  _ q W|  i |  } |  i | |  _ | Sd S(   s>    This only returns when amt has been read or socket times out i   N(   s   lens   selfs   inbufs   amts   recvs   data(   s   selfs   amts   data(    (    s   ./timeout_socket.pys   readË   s      c            s‘   x_ d oW t  i |  i d ƒ } | d j o Pn |  i d ƒ } | o Pn |  i | |  _ q W| d } |  i |  } |  i | |  _ | Sd S(   s#    readine for socket - buffers data i   s   
i    i   N(   s   strings   finds   selfs   inbufs   lfs   recvs   rs   data(   s   selfs   lfs   rs   data(    (    s   ./timeout_socket.pys   readlineÓ   s      
c            s:   g  t  i  |  i g g  g  t | ƒ p |  i ƒ d j Sd S(   s    returns 1/0 i    N(   s   selects   selfs   ss   ints   timeouts   _timeout(   s   selfs   timeout(    (    s   ./timeout_socket.pys   recvpendingè   s     c            s   t  | ƒ |  _ d S(   s    change socket timeout N(   s   ints   newtimos   selfs   _timeout(   s   selfs   newtimo(    (    s   ./timeout_socket.pys   timeoutì   s     c            s   d  S(   N(    (   s   self(    (    s   ./timeout_socket.pys   flushð   s    c            s   |  i | ƒ d  S(   N(   s   selfs   sends   data(   s   selfs   data(    (    s   ./timeout_socket.pys   writeô   s    (   s   __name__s
   __module__s   __doc__s   _TIMEOUTs   Nones   __init__s   __getattr__s   accepts   connects   sends   recvs   makefiles   closes   reads   readlines   recvpendings   timeouts   flushs   write(    (    (    s   ./timeout_socket.pys   timeout_socket>   s    		%	'	
				(   s   __doc__s   __version__s
   __author__s   sockets   errnos   selects   strings   _TIMEOUTs	   Exceptions   Timeouts   timeout_socket(	   s
   __author__s   strings   Timeouts   timeout_sockets   _TIMEOUTs   errnos   __version__s   selects   socket(    (    s   ./timeout_socket.pys   ?/   s   				