Python 和代理 - urllib2.URLError: <urlopen 错误 [Errno 110] 连接超时>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16775534/
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
Python and proxy - urllib2.URLError: <urlopen error [Errno 110] Connection timed out>
提问by Olexiy
I tried to google and search for similar question on stackOverflow, but still can't solve my problem.
我试图用谷歌搜索并在 stackOverflow 上搜索类似的问题,但仍然无法解决我的问题。
I need my python script to perform http connections via proxy.
Below is my test script: 
我需要我的 python 脚本来通过代理执行 http 连接。
下面是我的测试脚本:
import urllib2, urllib
proxy = urllib2.ProxyHandler({'http': 'http://255.255.255.255:3128'})
opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://www.whatismyip.com/')
return_str = conn.read()
webpage = open('webpage.html', 'w')
webpage.write(return_str)
webpage.close()
This script works absolutely fine on my local computer (Windows 7, Python 2.7.3), but when I try to run it on the server, it gives me the following error:
这个脚本在我的本地计算机(Windows 7,Python 2.7.3)上运行得非常好,但是当我尝试在服务器上运行它时,它给了我以下错误:
Traceback (most recent call last):
  File "proxy_auth.py", line 18, in <module>
    conn = urllib2.urlopen('http://www.whatismyip.com/')
  File "/home/myusername/python/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/home/myusername/python/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/home/myusername/python/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/home/myusername/python/lib/python2.7/urllib2.py", line 378, in _call_chai                                              n
    result = func(*args)
  File "/home/myusername/python/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/home/myusername/python/lib/python2.7/urllib2.py", line 1177, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 110] Connection timed out>
I also tried to use requestslibrary, and got the same error.
我也尝试使用请求库,并得到同样的错误。
# testing request library
r = requests.get('http://www.whatismyip.com/', proxies={'http':'http://255.255.255.255:3128'})
If I don't set proxy, then the program works fine.
如果我不设置代理,那么程序运行良好。
# this works fine
conn = urllib2.urlopen('http://www.whatismyip.com/')
I think the problem is that on my shared hosting account it is not possible to set an environment variable for proxy ... or something like that.
我认为问题在于,在我的共享主机帐户上,无法为代理设置环境变量......或类似的东西。
Are there any workarounds or alternative approaches that would let me set proxies for http connections? How should I modify my test script?
是否有任何解决方法或替代方法可以让我为 http 连接设置代理?我应该如何修改我的测试脚本?
采纳答案by Olexiy
The problem was in closed ports. I had to buy a dedicated IP before tech support could open the ports I needed.
问题出在封闭的端口上。在技术支持可以打开我需要的端口之前,我必须购买一个专用 IP。
Now my script works fine.
现在我的脚本工作正常。
Conclusion: when you are on a shared hosting, most ports are probably closed and you will have to contact tech support to open ports.
结论:当您使用共享主机时,大多数端口可能已关闭,您必须联系技术支持才能打开端口。

