PythonAnywhere上连接Twitter API被拒绝

3

我想连接Python anywhere上的Twitter流API,但总是收到拒绝连接错误。

我在我的应用程序中使用Tweepy,为了测试连接,我正在使用可以在该存储库中找到的流示例。

以下是代码摘要:

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

# Go to http://dev.twitter.com and create an app. 
# The consumer key and secret will be generated for you after
consumer_key=""
consumer_secret=""

# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=""
access_token_secret=""

class StdOutListener(StreamListener):
    """ A listener handles tweets are the received from the stream. 
    This is a basic listener that just prints received tweets to stdout.

    """
    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status

if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)    
    stream.filter(track=['basketball'])

当我在bash控制台中运行这条命令时(在填写了令牌之后),它是在PythonAnywhere上运行的。
12:02 ~/tweepy/examples (master)$ python streaming.py 

我收到了以下错误信息:
Traceback (most recent call last):
  File "streaming.py", line 33, in <module>
    stream.filter(track=['basketball'])
  File "/usr/local/lib/python2.7/site-packages/tweepy/streaming.py", line 228, in filter
    self._start(async)
  File "/usr/local/lib/python2.7/site-packages/tweepy/streaming.py", line 172, in _start
    self._run()
  File "/usr/local/lib/python2.7/site-packages/tweepy/streaming.py", line 106, in _run
    conn.connect()
  File "/usr/local/lib/python2.7/httplib.py", line 1157, in connect
    self.timeout, self.source_address)
  File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

尽管 .twitter.com 域名在 pythonanywhere 的白名单上,但我不明白为什么连接会被拒绝 :s。

同样的代码在我的 Ubuntu 上运行得非常好。

任何想法都将不胜感激,谢谢!!

2个回答

5
如果您使用的是免费账户,tweepy将无法使用。它不使用环境中的代理设置。
有一个tweepy的分支(http://github.com/ducu/tweepy),您可以尝试使用,直到主线正确使用代理设置为止。

嘿,谢谢你的回答!我看到一个支持代理的拉取请求已经在一个月前关闭了。也许我可以这样看? - jlengrand
是的,好的,一个关闭的拉取请求不会合并它:s。 这意味着它已被拒绝。 但我不知道为什么! - jlengrand

2
正如Glenn所说,目前tweepy没有代理支持。
由于某种无法解释(也没有记录)的原因,一个拉取请求大约一个月前被关闭了,但没有合并。

https://github.com/tweepy/tweepy/pull/152

“显然在 GitHub 上有一个分支(请参见 Glenn 的答案),但我没有测试过。”
“知道最终需要使用自己的域名,我最终在 PythonAnywhere 上购买了付费账户,并完全摆脱了代理的东西。”

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接