Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys, I am trying to solve one of the problems in the authentication section of pentesterlabs but I just can't seem to get it right. In essence, I am trying to use the urllib2 library in python for basic HTTP authentication, however despite providing the correct username and password I keep getting a 401: Unauthorized error. I even print out the header to make sure that it is correct, and I also base64 encoded it separately and it also seems to be correct. Can anybody please show me where I am going wrong?

Python
#The code I have written Authentication, HTTP, Basic, Authentication, so far
import urllib2
import base64
theurl = 'http://10.42.0.11/authentication/example2'
request = urllib2.Request(theurl)
try:
  handle = urllib2.urlopen(request)
except IOError, e:
  if hasattr(e, 'code'):
    if e.code != 401:
      print 'Something is seriously wrong, you got a different error.'
      print e.code
    else:
      print e.headers
      print e.headers['www-authenticate']
#The credentials for login in are hacker:pentesterlab
#Below you can see the base64 encoded version of the credentials
request.add_header("Authorization", "Basic %s"%"aGFja2VyOnBlbnRlc3RlcmxhYgo=") 
print request.headers
handle = urllib2.urlopen(request)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900