def open_with_auth(url):
 (scheme, netloc, path, params, query, frag) = urlparse.urlparse(url)
 if (scheme in ('http', 'https')):
  (auth, host) = urllib2.splituser(netloc)
 else:
  auth = None
 if auth:
  auth = ('Basic ' + urllib2.unquote(auth).encode('base64').strip())
  new_url = urlparse.urlunparse((scheme, host, path, params, query, frag))
  request = urllib2.Request(new_url)
  request.add_header('Authorization', auth)
 else:
  request = urllib2.Request(url)
 request.add_header('User-Agent', user_agent)
 fp = urllib2.urlopen(request)
 if auth:
  (s2, h2, path2, param2, query2, frag2) = urlparse.urlparse(fp.url)
  if ((s2 == scheme) and (h2 == host)):
   fp.url = urlparse.urlunparse((s2, netloc, path2, param2, query2, frag2))
 return fp

def open_with_auth ( url ) : ( scheme , netloc , path , params , query , frag ) = urlparse . urlparse ( url ) if ( scheme in ( 'http' , 'https' ) ) : ( auth , host ) = urllib2 . splituser ( netloc ) else : auth = None if auth : auth = ( 'Basic ' + urllib2 . unquote ( auth ) . encode ( 'base64' ) . strip ( ) ) new_url = urlparse . urlunparse ( ( scheme , host , path , params , query , frag ) ) request = urllib2 . Request ( new_url ) request . add_header ( 'Authorization' , auth ) else : request = urllib2 . Request ( url ) request . add_header ( 'User-Agent' , user_agent ) fp = urllib2 . urlopen ( request ) if auth : ( s2 , h2 , path2 , param2 , query2 , frag2 ) = urlparse . urlparse ( fp . url ) if ( ( s2 == scheme ) and ( h2 == host ) ) : fp . url = urlparse . urlunparse ( ( s2 , netloc , path2 , param2 , query2 , frag2 ) ) return fp
open a urllib2 request .