【发布时间】: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