在python 2中,可以通过
从urllib获取调试输出import httplib
import urllib
httplib.HTTPConnection.debuglevel = 1
response = urllib.urlopen('http://example.com').read()
但是,在python 3中,它看起来已经移到了
http.client.HTTPConnection.set_debuglevel(level)
但是,我直接使用urllib而不是http.client。如何设置它以便我的http请求以这种方式显示调试信息?
这是我到目前为止使用的内容。如果我想获得调试信息,最好的方法是什么?
#Request Login page
cookiejar = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar))
request = urllib.request.Request(options.uri)
add_std_headers(request)
response = opener.open(request)
response_string = response.read().decode("utf8")
# ...
答案 0 :(得分:16)
你第一次是对的。您只需在文件开头添加行http.client.HTTPConnection.debuglevel = 1
即可打开应用程序范围内的HTTP调试。 urllib.request
仍使用http.client
。
似乎还有一种方法可以为单个处理程序设置调试级别(通过创建urllib.request.HTTPHandler(debuglevel=1)
并使用它构建一个开启者),但是在我安装的Python3(3.0b3)中,它实际上并未实现。我想在最近的版本中已经改变了!
答案 1 :(得分:0)
在https://github.com/urllib3/urllib3/issues/107中,SmithSamuelM recommends:
if self.debug:
import httplib
httplib.HTTPConnection.debuglevel = 5
requests.packages.urllib3.add_stderr_logger()