打开 python 3 urllib 的调试输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/789856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Turning on debug output for python 3 urllib
提问by Zxaos
In python 2, it was possible to get debug output from urllib by doing
在python 2中,可以通过执行从urllib获得调试输出
import httplib
import urllib
httplib.HTTPConnection.debuglevel = 1
response = urllib.urlopen('http://example.com').read()
However, in python 3 it looks like this has been moved to
但是,在 python 3 中,它看起来已经被移动到
http.client.HTTPConnection.set_debuglevel(level)
However, I'm using urllib not http.client directly. How can I set it up so that my http request display debugging information in this way?
但是,我使用的是 urllib 而不是 http.client 直接。如何设置它以便我的 http 请求以这种方式显示调试信息?
Here's what I"m using so far. What's the best way to proceed if I want to be able to get debug information?
这是我目前使用的。如果我想获得调试信息,最好的方法是什么?
#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")
# ...
回答by PAG
You were right the first time. You can simply add the line http.client.HTTPConnection.debuglevel = 1
at the start of your file to turn on HTTP debugging application-wide. urllib.request
still uses http.client
.
你第一次是对的。您只需http.client.HTTPConnection.debuglevel = 1
在文件开头添加该行即可在应用程序范围内打开 HTTP 调试。urllib.request
仍然使用http.client
.
It seems that there's also a way to set the debuglevel for a single handler (by creating urllib.request.HTTPHandler(debuglevel=1)
and building an opener with that), but on my installation of Python3 (3.0b3) it's not actually implemented. I imagine that's changed in more recent versions!
似乎还有一种方法可以为单个处理程序设置调试级别(通过使用它创建urllib.request.HTTPHandler(debuglevel=1)
和构建开启程序),但是在我安装的 Python3 (3.0b3) 中,它实际上并未实现。我想这在最近的版本中有所改变!