utils.web: Simplify getUrlFd by using standard parse methods

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
James McCoy 2012-10-23 17:06:30 -04:00
parent 180508496f
commit 90b7f3cd4e

View File

@ -107,15 +107,13 @@ def getUrlFd(url, headers=None, data=None, timeout=None):
headers = defaultHeaders
try:
if not isinstance(url, urllib2.Request):
if '#' in url:
url = url[:url.index('#')]
(scheme, loc, path, query, frag) = urlparse.urlsplit(url)
(user, host) = urllib.splituser(loc)
url = urlparse.urlunsplit((scheme, host, path, query, ''))
request = urllib2.Request(url, headers=headers, data=data)
if '@' in url:
scheme, url = url.split('://', 2)
auth, url = url.split('@')
url = scheme + '://' + url
if user:
request.add_header('Authorization',
'Basic ' + base64.b64encode(auth))
'Basic %s' % base64.b64encode(user))
else:
request = url
request.add_data(data)