urllib2基本身份验证

2024-10-04 03:18:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取受密码保护的url

theurl = 'http://92.xx.xxx.x:80xx/connectioncounts'
username = ''
password = ''

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, theurl, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
opener.open(theurl)

但是总是得到urllib2.HTTPError:HTTP Error 401:Unauthorized

我在跟踪https://docs.python.org/2.7/howto/urllib2.html#id6

有什么问题吗?在


Tags: addhttpurlusernamepasswordopenerurllib2xxx