Question

I have a resource which is authenticated which requires realm, username and password for authentication. Python's urllib2 has provision to provide realm whereas i did not find any provision for httplib2. Here are sample codes, using urllib2 :

  auth_handler = urllib2.HTTPBasicAuthHandler()
  auth_handler.add_password("Protected",
                          uri="http://" + url,
                          user=username,
                          passwd=password)
  opener = urllib2.build_opener(auth_handler)
  urllib2.install_opener(opener)

  try :
     data = urllib2.urlopen(url, None, timeout).read()

Here "Protected" is the realm which is required for authentication.

Now look at httplib2 :

req = httplib2.Http(timeout=5)
req.add_credentials(username, password)
response, data = req.request (url, headers={'Connection': 'Keep-Alive', 'accept-encoding': 'gzip'})

In case of httplib2, i did not find any provision to provide realm string for authentication.

Is there a way to add this realm string in httplib2 ??

Was it helpful?

Solution

If httplib2 is not a must, using requests it becomes trivial:

requests.get('http://www.example.com/', auth=('user', 'pass'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top