【问题标题】:getting http debug info获取 http 调试信息
【发布时间】:2016-08-31 22:55:56
【问题描述】:

我正在通过 Dive into Python3。当我到达关于 http web 服务的章节 14.4 时,我似乎无法在 python3 shell 中复制以下输出。示例代码如下所示:

from http.client import HTTPConnection
HTTPConnection.debuglevel = 1
from urllib.request import urlopen
response = urlopen('http://diveintopython3.org/examples/feed.xml')

send: b'GET /examples/feed.xml HTTP/1.1
Host: diveintopython3.org
Accept-Encoding: identity
User-Agent: Python-urllib/3.1'
Connection: close
reply: 'HTTP/1.1 200 OK'
…further debugging information omitted…

当我在 ipython3 中输入这个时,最后的命令没有输出。那么为什么我没有得到示例中的调试信息呢?输入上述代码后,response.debuglevel == 0。我用的是python3.5.2。

【问题讨论】:

  • 你应该在 HTTPConnection 上设置调试级别,而不是在响应上
  • 你应该使用urllib2.urlopen(),因为urllib在Python3中被弃用了:docs.python.org/2/library/urllib.html
  • @alfasin 根据您自己的链接这是错误的:“另请注意,Python 3 中的 urllib.request.urlopen() 函数等效于 urllib2.urlopen() 并且 urllib.urlopen() 已经已删除。”
  • @alfasin 这正是我要说的,您没有正确阅读。它是说“Python 3 中的 urllib.request.urlopen() 函数等效于 [Python 2] 中的 urllib2.urlopen() 并且 [从 Python 3] 中删除了 urllib.urlopen()。” urllib 绝对还在 python 中(参见:docs.python.org/3.6/library/urllib.html),urllib2 不是 Python 3 中的模块。
  • @Jakub 你是对的 - 我的错!

标签: python python-3.x urllib http.client


【解决方案1】:

最后的命令不应该给出任何输出,你可能想要的是:

print(response.read())

【讨论】:

  • 当我在笔记本电脑上运行 python 代码时,它会给出问题中显示的调试输出。我检查了您链接的文档。 urllib 被拆分为 urllib.request、urllib.parse 和 urllib.error。看来 urllib2 实际上已被弃用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
相关资源
最近更新 更多