我正在尝试在pythonanywhere上部署我的应用程序,但我无法连接到twitter。
以下是我得到的错误: -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/btpraasta/btp_raasta/runServer.py", line 178, in remainingRequests
return (str)(getTrend.get_remainingRequests())
File "/home/btpraasta/btp_raasta/python/getTrend.py", line 20, in get_remainingRequests
data = get_json(url)
File "/home/btpraasta/btp_raasta/python/getTrend.py", line 10, in get_json
connection = urllib.urlopen(url, proxies=proxies)
File "/usr/lib/python2.7/urllib.py", line 87, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 208, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 437, in open_https
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 975, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 835, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 797, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 1178, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
IOError: [Errno socket error] [Errno 111] Connection refused
我试图运行的代码是: -
proxies = {'http': 'http://proxy.server:3128'}
connection = urllib.urlopen(url, proxies=proxies)
data = connection.read()
我正在尝试访问以下网址: - 'https://api.twitter.com/1.1/application/rate_limit_status.json'
我在pythonanywhere上有一个免费帐户,并确认该API已列入白名单,我在这里使用代理(http://help.pythonanywhere.com/pages/403ForbiddenError)。
答案 0 :(得分:2)
将我的评论复制到正确的答案:让代理与urllib
一起工作可能会非常棘手。 requests
通常更容易使用,它从环境变量中提取代理设置。绝对推荐!