Changeset 49592

Show
Ignore:
Timestamp:
10/16/06 11:38:45 (2 years ago)
Author:
bdelbosc
Message:

provide proxy_support (thanks to Greg) improve pylint score a bit

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • funkload/trunk/funkload/PatchWebunit.py

    r49367 r49592  
    4242from utils import thread_sleep 
    4343 
    44 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' 
    45 sep_boundary = '\r\n--' + boundary 
    46 end_boundary = sep_boundary + '--' 
    47 def mimeEncode(data, sep_boundary=sep_boundary, end_boundary=end_boundary): 
     44BOUNDARY = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' 
     45SEP_BOUNDARY = '\r\n--' + BOUNDARY 
     46END_BOUNDARY = SEP_BOUNDARY + '--' 
     47def mimeEncode(data, sep_boundary=SEP_BOUNDARY, end_boundary=END_BOUNDARY): 
    4848    '''Take the mapping of data and construct the body of a 
    4949    multipart/form-data message with it using the indicated boundaries. 
     
    5454            continue 
    5555        # handle multiple entries for the same name 
    56         if type(value) != type([]): value = [value] 
     56        if type(value) != type([]): 
     57            value = [value] 
    5758        for value in value: 
    5859            ret.write(sep_boundary) 
     
    8283 
    8384    def do_img(self, attributes): 
     85        """Process img tag.""" 
    8486        newattributes = [] 
    8587        for name, value in attributes: 
     
    108110 
    109111    def do_link(self, attributes): 
     112        """Process link tag.""" 
    110113        newattributes = [('rel', 'stylesheet'), ('type', 'text/css')] 
    111114        for name, value in attributes: 
     
    191194 
    192195    # TODO: allow override of the server and port from the URL! 
    193     if server is None: server = self.server 
    194     if port is None: port = self.port 
    195     if protocol is None: protocol = self.protocol 
    196     if ok_codes is None: ok_codes = self.expect_codes 
     196    if server is None: 
     197        server = self.server 
     198    if port is None: 
     199        port = self.port 
     200    if protocol is None: 
     201        protocol = self.protocol 
     202    if ok_codes is None: 
     203        ok_codes = self.expect_codes 
    197204 
    198205    if protocol == 'http': 
    199         h = httplib.HTTP(server, int(port)) 
     206        webproxy = {} 
     207        try: 
     208            proxystring = os.environ["http_proxy"].replace("http://", "") 
     209            webproxy['host'] = proxystring.split(":")[0] 
     210            webproxy['port'] = int(proxystring.split(":")[1]) 
     211        except (KeyError, IndexError, ValueError): 
     212            webproxy = False 
     213 
     214        if webproxy: 
     215            h = httplib.HTTPConnection(webproxy['host'], webproxy['port']) 
     216        else: 
     217            h = httplib.HTTP(server, int(port)) 
    200218        if int(port) == 80: 
    201219            host_header = server 
     
    225243        h.putrequest('POST', url) 
    226244        h.putheader('Content-type', 'multipart/form-data; boundary=%s'% 
    227             boundary
     245            BOUNDARY
    228246        h.putheader('Content-length', str(len(params))) 
    229247    else: 
    230         # Normal GET 
    231         h.putrequest('GET', url) 
     248        if webproxy: 
     249            h.putrequest('GET', "http://%s%s" % (server, url)) 
     250        else: 
     251            # Normal GET 
     252            h.putrequest('GET', url) 
    232253 
    233254    # Other Full Request headers 
     
    282303 
    283304    # handle the reply 
    284     errcode, errmsg, headers = h.getreply() 
    285  
    286     # get the body and save it 
    287     f = h.getfile() 
    288     g = cStringIO.StringIO() 
    289     d = f.read() 
    290     while d: 
    291         g.write(d) 
     305    if webproxy: 
     306        r = h.getresponse() 
     307        errcode = r.status 
     308        errmsg = r.reason 
     309        headers = r.msg 
     310        data = r.read() 
     311        response = HTTPResponse(self.cookies, protocol, server, port, url, 
     312                                errcode, errmsg, headers, data, 
     313                                self.error_content) 
     314 
     315    else: 
     316        # get the body and save it 
     317        errcode, errmsg, headers = h.getreply() 
     318        f = h.getfile() 
     319        g = cStringIO.StringIO() 
    292320        d = f.read() 
    293     response = HTTPResponse(self.cookies, protocol, server, port, url, 
    294         errcode, errmsg, headers, g.getvalue(), self.error_content) 
    295     f.close() 
     321        while d: 
     322            g.write(d) 
     323            d = f.read() 
     324        response = HTTPResponse(self.cookies, protocol, server, port, url, 
     325                                errcode, errmsg, headers, g.getvalue(), 
     326                                self.error_content) 
     327        f.close() 
    296328 
    297329    if errcode not in ok_codes: