HTTPHandler在py2和py3中的区别.
在Python2中,使用urlib2的接口,可以把收发包的内容直接打印在屏幕上,示例
import urllib2
httpHandler = urllib2.HTTPHandler(debuglevel=1)
httpsHandler = urllib2.HTTPSHandler(debuglevel=1)
opener = urllib2.build_opener(httpHandler, httpsHandler)
urllib2.install_opener(opener)
res = urllib2.urlopen('http://www.baidu.com')
Python3合并了urllib和urllib2的库
Python3版本
import urllib.request
http_handler = urllib.request.HTTPHandler()
https_handler = urllib.request.HTTPSHandler(debuglevel=1)
opener = urllib.request.build_opener(http_handler, https_handler)
urllib.request.install_opener(opener)
urllib.request.urlopen("http://www.baidu.com")
如果你只是想得到header信息的话
imoprt xxx
res = urllib.request.urlopen("http://www.baidu.com")
print(res.info())

浙公网安备 33010602011771号