<s> from httplib import HTTPConnection , HTTPSConnection , HTTPException <EOL> from urlparse import urlparse <EOL> from bencode import bdecode <EOL> import socket <EOL> from gzip import GzipFile <EOL> from StringIO import StringIO <EOL> from urllib import quote , unquote <EOL> from __init__ import product_name , version_short <EOL> VERSION = product_name + '<STR_LIT:/>' + version_short <EOL> MAX_REDIRECTS = <NUM_LIT:10> <EOL> class btHTTPcon ( HTTPConnection ) : <EOL> def connect ( self ) : <EOL> HTTPConnection . connect ( self ) <EOL> try : <EOL> self . sock . settimeout ( <NUM_LIT:30> ) <EOL> except : <EOL> pass <EOL> class btHTTPScon ( HTTPSConnection ) : <EOL> def connect ( self ) : <EOL> HTTPSConnection . connect ( self ) <EOL> try : <EOL> self . sock . settimeout ( <NUM_LIT:30> ) <EOL> except : <EOL> pass <EOL> class urlopen : <EOL> def __init__ ( self , url ) : <EOL> self . tries = <NUM_LIT:0> <EOL> self . _open ( url . strip ( ) ) <EOL> self . error_return = None <EOL> def _open ( self , url ) : <EOL> self . tries += <NUM_LIT:1> <EOL> if self . tries > MAX_REDIRECTS : <EOL> raise IOError , ( '<STR_LIT>' , <NUM_LIT> , <EOL> "<STR_LIT>" ) <EOL> ( scheme , netloc , path , pars , query , fragment ) = urlparse ( url ) <EOL> if scheme != '<STR_LIT:http>' and scheme != '<STR_LIT>' : <EOL> raise IOError , ( '<STR_LIT>' , '<STR_LIT>' , scheme , url ) <EOL> url = path <EOL> if pars : <EOL> url += '<STR_LIT:;>' + pars <EOL> if query : <EOL> url += '<STR_LIT:?>' + query <EOL> try : <EOL> if scheme == '<STR_LIT:http>' : <EOL> self . connection = btHTTPcon ( netloc ) <EOL> else : <EOL> self . connection = btHTTPScon ( netloc ) <EOL> self . connection . request ( '<STR_LIT:GET>' , url , None , <EOL> { '<STR_LIT>' : VERSION , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . response = self . connection . getresponse ( ) <EOL> except HTTPException , e : <EOL> raise IOError , ( '<STR_LIT>' , str ( e ) ) <EOL> status = self . response . status <EOL> if status in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> try : <EOL> self . connection . close ( ) <EOL> except : <EOL> pass <EOL> self . _open ( self . response . getheader ( '<STR_LIT>' ) ) <EOL> return <EOL> if status != <NUM_LIT:200> : <EOL> try : <EOL> data = self . _read ( ) <EOL> d = bdecode ( data ) <EOL> if d . has_key ( '<STR_LIT>' ) : <EOL> self . error_return = data <EOL> return <EOL> except : <EOL> pass <EOL> raise IOError , ( '<STR_LIT>' , status , self . response . reason ) <EOL> def read ( self ) : <EOL> if self . error_return : <EOL> return self . error_return <EOL> return self . _read ( ) <EOL> def _read ( self ) : <EOL> data = self . response . read ( ) <EOL> if self . response . getheader ( '<STR_LIT>' , '<STR_LIT>' ) . find ( '<STR_LIT>' ) >= <NUM_LIT:0> : <EOL> try : <EOL> compressed = StringIO ( data ) <EOL> f = GzipFile ( fileobj = compressed ) <EOL> data = f . read ( ) <EOL> except : <EOL> raise IOError , ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return data <EOL> def close ( self ) : <EOL> self . connection . close ( ) </s>
