parameters: (url, opener=urllib2.urlopen)
return_statement: return fp
docstring: Open a urllib2 request, handling HTTP authentication
docstring_summary: Open a urllib2 request, handling HTTP authentication
docstring_tokens: Open a urllib2 request handling HTTP authentication
function: def open_with_auth(url, opener=urllib2.urlopen):
      """Open a urllib2 request, handling HTTP authentication"""
  
      scheme, netloc, path, params, query, frag = urlparse(url)
  
      # Double scheme does not raise on Mac OS X as revealed by a
      # failing test. We would expect "nonnumeric port". Refs #20.
      if netloc.endswith(':'):
          raise httplib.InvalidURL("nonnumeric port: ''")
  
      if scheme in ('http', 'https'):
          auth, host = splituser(netloc)
      else:
          auth = None
  
      if auth:
          auth = "Basic " + _encode_auth(auth)
          new_url = 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 = opener(request)
  
      if auth:
          # Put authentication info back into request URL if same host,
          # so that links found on the page will work
          s2, h2, path2, param2, query2, frag2 = urlparse(fp.url)
          if s2==scheme and h2==host:
              fp.url = urlunparse((s2,netloc,path2,param2,query2,frag2))
  
      return fp
function_tokens: def open_with_auth ( url , opener = urllib2 . urlopen ) : scheme , netloc , path , params , query , frag = urlparse ( url ) # Double scheme does not raise on Mac OS X as revealed by a # failing test. We would expect "nonnumeric port". Refs #20. if netloc . endswith ( ':' ) : raise httplib . InvalidURL ( "nonnumeric port: ''" ) if scheme in ( 'http' , 'https' ) : auth , host = splituser ( netloc ) else : auth = None if auth : auth = "Basic " + _encode_auth ( auth ) new_url = 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 = opener ( request ) if auth : # Put authentication info back into request URL if same host, # so that links found on the page will work s2 , h2 , path2 , param2 , query2 , frag2 = urlparse ( fp . url ) if s2 == scheme and h2 == host : fp . url = urlunparse ( ( s2 , netloc , path2 , param2 , query2 , frag2 ) ) return fp