Python: URLError: <urlopen 错误 [Errno 10060]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15820739/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:05:46  来源:igfitidea点击:

Python: URLError: <urlopen error [Errno 10060]

pythonpython-2.7urllib2urllib

提问by Question3CPO

OS: Windows 7; Python 2.7.3 using the Python GUI Shell

操作系统:Windows 7;使用 Python GUI Shell 的 Python 2.7.3

I'm trying to read a website through Python, and several authors use the urlliband urllib2libraries. To store the site in a variable, I've seen a similar approach proposed:

我正在尝试通过 Python 阅读网站,并且有几位作者使用urlliburllib2库。要将站点存储在变量中,我看到了类似的方法建议:

import urllib
import urllib2
g = "http://www.google.com/"
read = urllib2.urlopen(g)

The last line generates an error after a 120+ seconds:

最后一行在 120+ 秒后生成错误:

> Traceback (most recent call last):   File "<pyshell#27>", line 1, in
> <module>
>     r = urllib2.urlopen(o)   File "C:\Python27\lib\urllib2.py", line 126, in urlopen
>     return _opener.open(url, data, timeout)   File "C:\Python27\lib\urllib2.py", line 400, in open
>     response = self._open(req, data)   File "C:\Python27\lib\urllib2.py", line 418, in _open
>     '_open', req)   File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
>     result = func(*args)   File "C:\Python27\lib\urllib2.py", line 1207, in http_open
>     return self.do_open(httplib.HTTPConnection, req)   File "C:\Python27\lib\urllib2.py", line 1177, in do_open
>     raise URLError(err) URLError: <urlopen error [Errno 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>

I tried bypassing the gvariable and trying to urlopen("http://www.google.com/")with no success either (it generates the same error after the same length of time).

我尝试绕过g变量并尝试也urlopen("http://www.google.com/")没有成功(它在相同的时间长度后产生相同的错误)。

采纳答案by Sheng

The error code 10060 means it cannot connect to the remote peer. It might be because of the network problem or mostly your setting issues, such as proxy setting.

错误代码 10060 表示它无法连接到远程对等方。这可能是因为网络问题或主要是您的设置问题,例如代理设置。

You could try to connect the same host with other tools(such as ncat) and/or with another PC within your same local network to find out where the problem is occuring.

您可以尝试将同一台主机与其他工具(例如 ncat)和/或同一本地网络中的另一台 PC 连接,以找出问题发生的位置。

For proxy issue, there are some material here:

对于代理问题,这里有一些材料:

Using an HTTP PROXY - Python

使用 HTTP 代理 - Python

Why can't I get Python's urlopen() method to work on Windows?

为什么我不能让 Python 的 urlopen() 方法在 Windows 上工作?

Hope it helps!

希望能帮助到你!

回答by RANA RAUFF

This is because of the proxy settings. I also had the same problem, under which I could not use any of the modules which were fetching data from the internet. There are simple steps to follow:
1.open the control panel
2.open internet options
3.under connection tab open LAN settings
4.go to advance settings and unmark everything, delete every proxy in there. Or u can just unmark the checkbox in proxy server this will also do the same
5.save all the settingsby clicking ok.
you are done. try to run the programme again, it must work it worked for me at least

这是因为代理设置。我也遇到了同样的问题,在这种情况下,我无法使用任何从 Internet 获取数据的模块。遵循简单的步骤:
1.打开控制面板
2.打开Internet 选项
3.在连接选项卡下打开 LAN 设置
4.转到高级设置并取消标记所有内容删除其中的每个代理。或者您可以取消选中代理服务器中的复选框,这也将执行相同的操作
5.单击确定保存所有设置
你完成了。尝试再次运行该程序,它必须至少对我有用

回答by DataFramed

Answer (Basic is advance!):

答案(基本是提前!):

Error: 10060Adding a timeout parameter to request solved the issue for me.

错误:10060向请求添加超时参数为我解决了这个问题。

Example 1

示例 1

import urllib
import urllib2
g = "http://www.google.com/"
read = urllib2.urlopen(g, timeout=20)

Example 2

示例 2

A similar error also occurred while I was making a GET request. Again, passing a timeoutparameter solved the 10060 Error.

在我发出 GET 请求时也发生了类似的错误。同样,传递timeout参数解决了 10060 错误。

response = requests.get(param_url, timeout=20)