On the Mac, use Internet Config to find the proxies (Jack Jansen).

Also added two XXX comments about lingering thread unsafeness.
This commit is contained in:
Guido van Rossum 1998-08-06 13:39:09 +00:00
parent 4ff6d27319
commit 4163e708ed

View file

@ -104,6 +104,7 @@ class URLopener:
# Undocumented feature: you can use a different # Undocumented feature: you can use a different
# ftp cache by assigning to the .ftpcache member; # ftp cache by assigning to the .ftpcache member;
# in case you want logically independent URL openers # in case you want logically independent URL openers
# XXX This is not threadsafe. Bah.
def __del__(self): def __del__(self):
self.close() self.close()
@ -345,6 +346,7 @@ class URLopener:
dirs, file = dirs[:-1], dirs[-1] dirs, file = dirs[:-1], dirs[-1]
if dirs and not dirs[0]: dirs = dirs[1:] if dirs and not dirs[0]: dirs = dirs[1:]
key = (user, host, port, string.joinfields(dirs, '/')) key = (user, host, port, string.joinfields(dirs, '/'))
# XXX thread unsafe!
if len(self.ftpcache) > MAXFTPCACHE: if len(self.ftpcache) > MAXFTPCACHE:
# Prune the cache, rather arbitrarily # Prune the cache, rather arbitrarily
for k in self.ftpcache.keys(): for k in self.ftpcache.keys():
@ -908,21 +910,53 @@ def urlencode(dict):
# Proxy handling # Proxy handling
def getproxies(): if os.name == 'mac':
"""Return a dictionary of protocol scheme -> proxy server URL mappings. def getproxies():
"""Return a dictionary of scheme -> proxy server URL mappings.
Scan the environment for variables named <scheme>_proxy; By convention the mac uses Internet Config to store
this seems to be the standard convention. If you need a proxies. An HTTP proxy, for instance, is stored under
different way, you can pass a proxies dictionary to the the HttpProxy key.
[Fancy]URLopener constructor.
""" """
proxies = {} try:
for name, value in os.environ.items(): import ic
name = string.lower(name) except ImportError:
if value and name[-6:] == '_proxy': return {}
proxies[name[:-6]] = value
return proxies try:
config = ic.IC()
except ic.error:
return {}
proxies = {}
# HTTP:
if config.has_key('UseHTTPProxy') and config['UseHTTPProxy']:
try:
value = config['HTTPProxyHost']
except ic.error:
pass
else:
proxies['http'] = 'http://%s' % value
# FTP: XXXX To be done.
# Gopher: XXXX To be done.
return proxies
else:
def getproxies():
"""Return a dictionary of scheme -> proxy server URL mappings.
Scan the environment for variables named <scheme>_proxy;
this seems to be the standard convention. If you need a
different way, you can pass a proxies dictionary to the
[Fancy]URLopener constructor.
"""
proxies = {}
for name, value in os.environ.items():
name = string.lower(name)
if value and name[-6:] == '_proxy':
proxies[name[:-6]] = value
return proxies
# Test and time quote() and unquote() # Test and time quote() and unquote()