【发布时间】:2012-04-17 15:49:30
【问题描述】:
我正在尝试编写一个简单地显示网站标题信息的小程序。代码如下:
import urllib2
url = 'http://some.ip.add.ress/'
request = urllib2.Request(url)
try:
html = urllib2.urlopen(request)
except urllib2.URLError, e:
print e.code
else:
print html.info()
如果 'some.ip.add.ress' 是 google.com,则返回标头信息没有问题。但是,如果它是一个需要在访问之前进行基本身份验证的 IP 地址,那么它会返回 401。有没有办法在没有身份验证的情况下获取标头(或任何其他)信息?
我已经解决了。
由于未经授权的访问而尝试失败后,以下修改将打印头信息:
print e.info()
代替:
print e.code()
感谢收看 :)
【问题讨论】:
-
没关系,我已经解决了。在错误时打印 e.info() 仍在学习 python....
-
发布您的解决方案作为此问题的答案。
标签: python http authentication http-headers