- 论坛徽章:
- 0
|
import urllib2
password_mgr=urllib2.HTTPPasswordMgrWithDefaultRealm()
url = "http://web.TEST.com"
a_url = "http://web.TEST.com/Pages/index.htm"
username = "ua\user"
password = "password"
password_mgr.add_password(None,url,username,password)
handler=urllib2.HTTPBasicAuthHandler(password_mgr)
opener=urllib2.build_opener(handler)
opener.open(a_url)
urllib2.install_opener(opener)
运行结果:
E:\Python25\TEMP>test.py
Traceback (most recent call last):
File "E:\Python25\TEMP\signout.py", line 13, i
opener.open(a_url)
File "E:\Python25\lib\urllib2.py", line 387, i
response = meth(req, response)
File "E:\Python25\lib\urllib2.py", line 498, i
'http', request, response, code, msg, hdrs)
File "E:\Python25\lib\urllib2.py", line 425, i
return self._call_chain(*args)
File "E:\Python25\lib\urllib2.py", line 360, i
result = func(*args)
File "E:\Python25\lib\urllib2.py", line 506, i
raise HTTPError(req.get_full_url(), code, ms
urllib2.HTTPError: HTTP Error 401: Unauthorized
这段代码对apache的web服务器是有效的,现在测试的web服务器是IIS 不知道是否原因在这里,还是在有户名处存在“\”. 固提交失败。誰能帮我分析下。谢谢 :) |
|