3

预先感谢您提供的任何帮助。

我在安装了 python 2.7 的 ubuntu 12.10 上。我根据其他帖子编写了一个简单的脚本来测试 http 和 https 连接:

import urllib2, urllib

def set_proxy():
  proxy = urllib2.ProxyHandler({'http': 'http://<proxyhost>:<proxyport>'})
  opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
  urllib2.install_opener(opener)

def http_call():
  conn = urllib2.urlopen('http://www.whatismyip.com/')
  return conn.read()

def https_call():
  conn = urllib2.urlopen('https://chase.com/')
  return conn.read()

set_proxy()
webpage = open('webpage.html', 'w')
return_str = https_call()
webpage.write(return_str)
webpage.close()
print ("check for output in webpage.html")

使用 http 的测试工作正常,但使用 https 会产生以下输出:

Traceback (most recent call last):
File "test.py", line 18, in <module>
  return_str = https_call()
File "test.py", line 13, in https_call
  conn = urllib2.urlopen('https://chase.com/')
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
  return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 401, in open
  response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 419, in _open
  '_open', req)
File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
  result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1219, in https_open
  return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1181, in do_open
  raise URLError(err)
urllib2.URLError: <urlopen error [Errno 110] Connection timed out>

这是我的设置:

root@sc11137376:/usr/local/pythonbrew#  lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.10
Release:        12.10
Codename:       quantal

root@sc11137376:/usr/local/pythonbrew# python --version
Python 2.7.3

root@sc11137376:/usr/local/pythonbrew# openssl version
OpenSSL 1.0.1c 10 May 2012

我在类似的较早的帖子中看到了从安装了 openssl 的源代码重建 python 的建议。我希望有一个不同的解决方案来解决我的问题,因为这是更新的 ubuntu/python 版本,并且 openssl 已经在系统上。

任何指针表示赞赏。

注意:在环境中设置 HTTPS_PROXY 会将错误消息更改为以下内容(从错误号 110 到 113):

urllib2.URLError: <urlopen error [Errno 113] No route to host>

顺便说一句,以下也失败了:

root@sc11137376:/usr/local/pythonbrew# openssl s_client -connect encrypted.google.com:443
connect: No route to host
connect:errno=113

不确定我是否可以做任何事情来解决这个问题。

4

1 回答 1

0

URLError:urlopen 错误 [Errno 113] 没有到主机的路由

我遇到了同样的错误:我想在机器 B 上从机器 A 远程执行一些脚本(它会执行一些浏览器自动化操作),最后我遇到了上述错误,后来我禁用了 HTTPS 的防火墙设置。

我如何禁用 RHEL6.4 上的防火墙设置?- 单击设置(在面板左侧)> 管理 > 防火墙 > 在“防火墙配置”窗口中:检查“安全 WWW (HTTPS)” -(您需要是 root 用户)>> 单击“应用”>> 单击禁用按钮。

后来我能够毫不费力地在远程机器 B 上执行脚本。

于 2015-09-29T08:45:18.600 回答