【问题标题】:Python urllib.request.urlopen() returning error 10061?Python urllib.request.urlopen() 返回错误 10061?
【发布时间】:2012-03-06 15:43:11
【问题描述】:

我正在尝试下载页面的 HTML(在这种情况下为http://www.google.com),但我收到了一个错误。这是我的交互式提示会话:

Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import urllib.request
>>> html = urllib.request.urlopen("http://www.google.com")
Traceback (most recent call last):
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1136, in
 do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 964, in req
uest
    self._send_request(method, url, body, headers)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 1002, in _s
end_request
    self.endheaders(body)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 960, in end
headers
    self._send_output(message_body)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 805, in _se
nd_output
    self.send(msg)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 743, in sen
d
    self.connect()
  File "\\****.****.org\myhome\python\lib\http\client.py", line 721, in con
nect
    self.timeout, self.source_address)
  File "\\****.****.org\myhome\python\lib\socket.py", line 404, in create_c
onnection
    raise err
  File "\\****.****.org\myhome\python\lib\socket.py", line 395, in create_c
onnection
    sock.connect(sa)
socket.error: [Errno 10061] No connection could be made because the target machi
ne actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 138, in
urlopen
    return opener.open(url, data, timeout)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 369, in
open
    response = self._open(req, data)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 387, in
_open
    '_open', req)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 347, in
_call_chain
    result = func(*args)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1156, in
 http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1139, in
 do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 10061] No connection could be made
because the target machine actively refused it>
>>>

我最好的猜测是我的网络防火墙阻止了连接(除 80 之外的大多数/所有端口都被阻止)。不过,我不明白为什么; Python 没有连接到 80 端口吗?有谁知道发生了什么?

【问题讨论】:

标签: python urllib


【解决方案1】:

urllib 从 Internet Explorer 获取代理设置,通常在 Tools-&gt;Internet Options-&gt;Connections-&gt;Lan Settings

如果您有代理,请确保它在 IE 中是正确的,或 set it when using urllib。如果不使用代理浏览,请确保IE中的代理设置为空。

我在这个问题上苦苦挣扎了几个小时,直到我意识到我曾经安装的第 3 方应用程序更改了 IE 中的设置,而我在没有代理的情况下在 Firefox 中愉快地冲浪。


另一个解决方案是the this link,它显示了如何强制 urllib 忽略 IE 的代理设置

【讨论】:

    【解决方案2】:

    错误来自操作系统级别,与 Python 无关。任何编程语言都一样。

    除非您自己解决防火墙问题,否则请与您的网络管理员联系以了解问题所在。如果你需要一个 HTTP 代理,那么你可以为 urllib 配置一个。

    【讨论】:

    • 我自己也遇到了这个问题,值得知道windows中的http_proxy环境变量导致python的urllib出现问题。删除它解决了问题
    猜你喜欢
    • 2016-01-06
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多