Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d52755f

Browse files
committed
Provide a clearer error message when urlopen fails because of an
invalid proxy setting. Minor change to call of unknown_url; always pass data argument explicitly since data defaults to None. PEP 42: Add as a feature that urllib handle proxy setting that contain only the host and port of the proxy.
1 parent 4dc1a6d commit d52755f

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

Lib/urllib.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,23 @@ def open(self, fullurl, data=None):
142142
fp = open(filename, 'rb')
143143
return addinfourl(fp, headers, fullurl)
144144
type, url = splittype(fullurl)
145-
if not type: type = 'file'
145+
if not type:
146+
type = 'file'
146147
if self.proxies.has_key(type):
147148
proxy = self.proxies[type]
148-
type, proxy = splittype(proxy)
149-
host, selector = splithost(proxy)
149+
type, proxyhost = splittype(proxy)
150+
host, selector = splithost(proxyhost)
150151
url = (host, fullurl) # Signal special case to open_*()
152+
else:
153+
proxy = None
151154
name = 'open_' + type
152155
self.type = type
153156
if '-' in name:
154157
# replace - with _
155158
name = string.join(string.split(name, '-'), '_')
156159
if not hasattr(self, name):
157-
if data is None:
158-
return self.open_unknown(fullurl)
160+
if proxy:
161+
return self.open_unknown_proxy(proxy, fullurl, data)
159162
else:
160163
return self.open_unknown(fullurl, data)
161164
try:
@@ -171,6 +174,11 @@ def open_unknown(self, fullurl, data=None):
171174
type, url = splittype(fullurl)
172175
raise IOError, ('url error', 'unknown url type', type)
173176

177+
def open_unknown_proxy(self, proxy, fullurl, data=None):
178+
"""Overridable interface to open unknown URL type."""
179+
type, url = splittype(fullurl)
180+
raise IOError, ('url error', 'invalid proxy for %s' % type, proxy)
181+
174182
# External interface
175183
def retrieve(self, url, filename=None, reporthook=None, data=None):
176184
"""retrieve(url) returns (filename, None) for a local object

0 commit comments

Comments
 (0)