问题描述
嘿伙计们,我正在尝试解决pentesterlabs认证部分中的一个问题,但我似乎无法做到正确。本质上,我试图在python中使用urllib2库进行基本的HTTP身份验证,但是尽管提供了正确的用户名和密码,我仍然会收到401:Unauthorized错误。我甚至打印出标题以确保它是正确的,我也对它进行了单独编码,它似乎也是正确的。有人可以告诉我我哪里错了吗?
# 我编写的代码验证,HTTP,基本,身份验证,到目前为止
import urllib2
import base64
theurl = ' http://10.42.0.11/authentication/example2'
request = urllib2.Request(theurl)
try :
handle = urllib2.urlopen(request)
IOError,e:
if hasattr(e,' code'):
if e.code!= 401 :
print ' 有些东西是se出了问题,你得到了一个不同的错误。'
print e.code
否则:
print e.headers
print e.headers [' www-authenticate']
# 登录的凭据是黑客:pentesterlab
# 下面你可以看到凭证的base64编码版本
request.add_header( 授权, 基本%s% aGFja2VyOnBlbnRlc3RlcmxhYgo =)
print request.headers
handle = urllib2.urlo笔(请求)
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?
#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)
这篇关于如何在python中使用urllib2进行基本HTTTP身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!