2

Twitter のストリーミング API フィルター ストリームと組み合わせて pycurl を使用すると問題が発生します。以下のコードを実行すると何が起こっているのか、実行呼び出しでバーフしているようです。これは、実行呼び出しの前後に print ステートメントを配置したためです。私は Python 2.6.1 を使用しており、それが重要な場合は Mac を使用しています。

#!/usr/bin/python
print "Content-type: text/html"
print
import pycurl, json, urllib

STREAM_URL = "http://stream.twitter.com/1/statuses/filter.json?follow=1&count=100"
USER = "user"
PASS = "password"
print "<html><head></head><body>"


class Client:
    def __init__(self):
        self.buffer = ""
        self.conn = pycurl.Curl()
        self.conn.setopt(pycurl.POST,1)
        self.conn.setopt(pycurl.USERPWD, "%s:%s" % (USER,PASS))
        self.conn.setopt(pycurl.URL, STREAM_URL)
        self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive)

        try:
            self.conn.perform()
            self.conn.close()
        except BaseException:
            traceback.print_exc()

    def on_receive(self,data):
        self.buffer += data
        if data.endswith("\r\n") and self.buffer.strip():
            content = json.loads(self.buffer)
            self.buffer = ""
            print content
            if "text" in content:
                print u"{0[user][name]}: {0[text]}".format(content)

client = Client()

print "</body></html>"
4

2 に答える 2

2

まず、デバッグを支援するために冗長性をオンにしてみてください。

    self.conn.setopt(pycurl.VERBOSE ,1)

基本認証モードを設定していないようです:

    self.conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)

また、ドキュメントによると、パラメーターの POST を API に提供する必要があり、GET パラメーターとして渡す必要はありません。

    data = dict( track='stack overflow' )
    self.conn.setopt(pycurl.POSTFIELDS,urlencode(data))
于 2011-02-18T01:09:55.213 に答える
1

基本認証を使用しようとしています。

基本認証は、HTTP 要求のヘッダーでユーザー資格情報を送信します。これにより、使いやすくなりますが、安全ではありません。OAuth は、今後 Twitter で推奨される認証方法です。2010年 8 月には、API からの基本認証を無効にします。--認証、Twitter

于 2010-11-27T11:49:28.420 に答える