home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_PPM_ppmhttplib.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  1.4 KB  |  44 lines

  1. from httplib import HTTP
  2. import string,os
  3. import urllib
  4.  
  5. # Proxy handling
  6. def getproxies_environment():
  7.     """Return a dictionary of scheme -> proxy server URL mappings.
  8.  
  9.     Scan the environment for variables named <scheme>_proxy;
  10.     this seems to be the standard convention. 
  11.     """
  12.     proxies = {}
  13.     for name, value in os.environ.items():
  14.         name = string.lower(name)
  15.         if value and name[-6:] == '_proxy':
  16.             proxies[name[:-6]] = value
  17.     return proxies
  18.  
  19. class PPMHTTP(HTTP):
  20.     """this is a subclass of standard HTTP class which also connects
  21.     through a HTTP proxy server."""
  22.     
  23.     def __init__(self, host='', port=None, **x509):
  24.         self.hostname = host
  25.         self.proxyhost = None
  26.         # get the proxy host from env
  27.         proxies = urllib.getproxies()
  28.         if proxies.has_key('http'):
  29.            proxy = proxies['http']
  30.            type, proxyurl = urllib.splittype(proxy)
  31.            self.proxyhost, selector = urllib.splithost(proxyurl)
  32.                
  33.         if self.proxyhost:
  34.             HTTP.__init__(self, self.proxyhost, port, **x509)
  35.         else:
  36.             HTTP.__init__(self, self.hostname, port, **x509)
  37.  
  38.     def postrequest(self, method, url):
  39.         if self.proxyhost:
  40.             import urlparse
  41.             url = urlparse.urljoin('http://'+self.hostname,url)
  42.  
  43.         self.putrequest(method, url)
  44.