<s> import logging <EOL> import random <EOL> import select <EOL> import socket <EOL> import ssl <EOL> import time <EOL> import cStringIO <EOL> from socketpool import Connector <EOL> from socketpool . util import is_connected <EOL> CHUNK_SIZE = <NUM_LIT:16> * <NUM_LIT> <EOL> MAX_BODY = <NUM_LIT> * <NUM_LIT> <EOL> DNS_TIMEOUT = <NUM_LIT> <EOL> class Connection ( Connector ) : <EOL> def __init__ ( self , host , port , backend_mod = None , pool = None , <EOL> is_ssl = False , extra_headers = [ ] , proxy_pieces = None , timeout = None , <EOL> ** ssl_args ) : <EOL> self . _s = backend_mod . Socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> if timeout is not None : <EOL> self . _s . settimeout ( timeout ) <EOL> self . _s . connect ( ( host , port ) ) <EOL> if proxy_pieces : <EOL> self . _s . sendall ( proxy_pieces ) <EOL> response = cStringIO . StringIO ( ) <EOL> while response . getvalue ( ) [ - <NUM_LIT:4> : ] != '<STR_LIT>' : <EOL> response . write ( self . _s . recv ( <NUM_LIT:1> ) ) <EOL> response . close ( ) <EOL> if is_ssl : <EOL> self . _s = ssl . wrap_socket ( self . _s , ** ssl_args ) <EOL> self . extra_headers = extra_headers <EOL> self . is_ssl = is_ssl <EOL> self . backend_mod = backend_mod <EOL> self . host = host <EOL> self . port = port <EOL> self . _connected = True <EOL> self . _life = time . time ( ) - random . randint ( <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> self . _pool = pool <EOL> self . _released = False <EOL> def matches ( self , ** match_options ) : <EOL> target_host = match_options . get ( '<STR_LIT:host>' ) <EOL> target_port = match_options . get ( '<STR_LIT:port>' ) <EOL> return target_host == self . host and target_port == self . port <EOL> def is_connected ( self ) : <EOL> if self . _connected : <EOL> return is_connected ( self . _s ) <EOL> return False <EOL> def handle_exception ( self , exception ) : <EOL> raise <EOL> def get_lifetime ( self ) : <EOL> return self . _life <EOL> def invalidate ( self ) : <EOL> self . close ( ) <EOL> self . _connected = False <EOL> self . _life = - <NUM_LIT:1> <EOL> def release ( self , should_close = False ) : <EOL> if self . _pool is not None : <EOL> if self . _connected : <EOL> if should_close : <EOL> self . invalidate ( ) <EOL> self . _pool . release_connection ( self ) <EOL> else : <EOL> self . _pool = None <EOL> elif self . _connected : <EOL> self . invalidate ( ) <EOL> def close ( self ) : <EOL> if not self . _s or not hasattr ( self . _s , "<STR_LIT>" ) : <EOL> return <EOL> try : <EOL> self . _s . close ( ) <EOL> except : <EOL> pass <EOL> def socket ( self ) : <EOL> return self . _s <EOL> def send_chunk ( self , data ) : <EOL> chunk = "<STR_LIT>" . join ( ( "<STR_LIT>" % len ( data ) , data , "<STR_LIT:\r\n>" ) ) <EOL> self . _s . sendall ( chunk ) <EOL> def send ( self , data , chunked = False ) : <EOL> if chunked : <EOL> return self . send_chunk ( data ) <EOL> return self . _s . sendall ( data ) <EOL> def sendlines ( self , lines , chunked = False ) : <EOL> for line in list ( lines ) : <EOL> self . send ( line , chunked = chunked ) <EOL> def sendfile ( self , data , chunked = False ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> data . seek ( <NUM_LIT:0> ) <EOL> while True : <EOL> binarydata = data . read ( CHUNK_SIZE ) <EOL> if binarydata == '<STR_LIT>' : <EOL> break <EOL> self . send ( binarydata , chunked = chunked ) <EOL> def recv ( self , size = <NUM_LIT> ) : <EOL> return self . _s . recv ( size ) </s>
