打开python 3 urllib的调试输出 [英] Turning on debug output for python 3 urllib

查看:271
本文介绍了打开python 3 urllib的调试输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python 2中,可以通过执行

In python 2, it was possible to get debug output from urllib by doing

import httplib
import urllib
httplib.HTTPConnection.debuglevel = 1
response = urllib.urlopen('http://example.com').read()

但是,在python 3中,它看起来已经被移动到

However, in python 3 it looks like this has been moved to

http.client.HTTPConnection.set_debuglevel(level)

但是,我使用urllib不是http。客户端直接如何设置它,以便我的http请求以这种方式显示调试信息?

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?

这是我使用到目前为止,最好的方法是继续,如果我想要获得调试信息?

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")
# ...


推荐答案

你是第一次正确,你可以简单地添加行 http.client.HTTPConnection.debuglevel = 1 在您的文件开始时启用HTTP调试应用程序范围。 urllib.request 仍然使用 http.client

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.

似乎还有一种方法来为单个处理程序设置调试级别(由cre at $ urllib.request.HTTPHandler(debuglevel = 1)并构建一个开启者),但是在我的Python3(3.0b3)安装中,实际上并没有实现。我想像在最近的版本中发生了变化!

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!

这篇关于打开python 3 urllib的调试输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆