1
我在Windows XP PC上使用帶有VitualBox的Ubuntu 12。我正在使用代理訪問互聯網。 我可以用Firefox瀏覽這些網站,以及下面的腳本:Ubuntu VM上的Python腳本無法連接到Twitter API
import urllib2
import json
proxy = urllib2.ProxyHandler({'http': 'http://my-proxy-server:80'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
#conn = urllib2.urlopen('http://python.org')
return_str = conn.read()
print return_str
一切順利碼以上,但對於如下類似的代碼無法正常工作。
import urllib2
import json
proxy = urllib2.ProxyHandler({'http': 'http://my-proxy-server:80'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
response = urllib.urlopen('https://api.twitter.com/1.1/search/tweets.json?q=microsoft')
print json.load(response)
錯誤日誌如下。
Traceback (most recent call last):
File "print.py", line 12, in <module>
conn = urllib2.urlopen('https://api.twitter.com/1.1/search/tweets.json?q=microsoft')
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 113] No route to host>
請幫幫我。謝謝。
我得到了 urllib2.HTTPError:HTTP錯誤400:錯誤的請求 –