Python和代理-urllib2.urleror:<;urlopen错误[Errno 110]连接超时>;

Python和代理-urllib2.urleror:<;urlopen错误[Errno 110]连接超时>;,python,urllib2,Python,Urllib2,我试着用谷歌搜索stackOverflow上类似的问题,但仍然无法解决我的问题 我需要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

我试着用谷歌搜索stackOverflow上类似的问题,但仍然无法解决我的问题

我需要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()
该脚本在我的本地计算机(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>
如果我不设置代理,那么程序就可以正常运行

# this works fine
conn = urllib2.urlopen('http://www.whatismyip.com/')
我认为问题在于,在我的共享主机帐户上,无法为代理设置环境变量。。。或者类似的


是否有任何变通方法或替代方法可以让我为http连接设置代理?如何修改测试脚本?

问题出在关闭的端口中。 在技术支持人员打开我需要的端口之前,我必须购买一个专用IP

现在我的脚本运行良好

# this works fine
conn = urllib2.urlopen('http://www.whatismyip.com/')

结论:当您在共享主机上时,大多数端口可能已关闭,您必须联系技术支持以打开端口。

在服务器上ping www.whatismyip.com
ping:icmp open socket:不允许操作
服务器是什么操作系统?在我看来,服务器似乎没有按照您期望的方式配置为允许http请求。你还说“如果我不设置代理,那么程序就可以工作”——所以…不要设置代理,享受一个工作程序!!??!!?亲爱的沃斯普林!据我所知,操作系统是Debian。我使用代理是有原因的,所以你关于“不设置代理”的建议没有太大帮助,也不合适。我在我的托管提供商的技术支持上打开了一张罚单,他们回答说,他们服务器上的大多数端口默认关闭,要打开端口,我需要购买专用IP。