Python 3.4 - 无法使用 urllib 连接到 http

Python 3.4 - failing to connect to http with urllib

我正在尝试 运行 一个简单的 Python 脚本,它连接到网站并读取网站上的文档,但它失败了。任何帮助,将是最欢迎的,谢谢!

代码如下:

import urllib.request

fhand = urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')

for line in fhand:
    print (line.strip())

我收到以下两条错误消息:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

urllib.error.URLError: {urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond}

这是 windows 错误,不是 urllib 的错误。后者 , but the former depends on settings of your proxy(初始连接默认为 60 秒,任何进一步的 GET 请求默认为 120 秒。

我对 windows 帮不上什么忙,但至少现在你知道去哪里找了。

我有同样的问题,并尝试在 urllib.request.urlopen 中指定超时,但没有骰子。

有效的是包括 header,如 here 所述。第一个答案有效,第二个也有效,所以我使用了更简单的第二个解决方案。