3

継続するGETリクエストを実行する疑似モジュールを作成したいと思います(Twitter Streaming APIを使用するモジュールとほぼ同じです)が、誰かが関数を呼び出したいときにすべてのパラメーターを指定する必要はありません。同じGETリクエストを行います。

私のmodule.pyには

class viewResults():
    def __init__(self,username,password,keyname,consume):
        self.buffer = ""
        self.consume = consume
        self.conn = pycurl.Curl()  
        self.conn.setopt(pycurl.USERPWD, "%s:%s" % (username, password))  
        self.conn.setopt(pycurl.URL, "http://crowdprocess.no.de/"+keyname+"/results") 
        self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive)
    #   self.conn.setopt(pycurl.VERBOSE, 1)
    #   self.conn.setopt(pycurl.DEBUGFUNCTION, self.debug)
        self.conn.perform()

#   def debug(self,debug_type,debug_message):
#       print 'type: '+str(debug_type)+' message'+str(debug_message)

    def on_receive(self, data):  
        self.buffer += data  
        if data.endswith("\r\n") and self.buffer.strip():  
            content = json.loads(self.buffer)
            self.consume(content)
            self.buffer = ""

そしてindex.pyに私は持っています

from module import viewResults

def consume(content):
    print content

viewResults('username','password','keyname',consume)

そのため、パラメーターusername、password、keyname、およびバッファーが有効なJSONデータでいっぱいになったときに呼び出される「consume」関数のみを渡したいと思いました...

何が起こっているのかというと、要求が実際に行われ、VERBOSEがオンの場合、到着するすべてのデータを確認できますが、その「高レベルの消費」関数は何も取得しません...

どうすればこれを達成できますか?ありがとう。

4

2 に答える 2

1

私が理解しているように、デバッグデータをアーカイブしたいですか?

データを保存するカスタム デバッグ関数を作成します: custom_debug(debug_type, debug_msg)

>>> import human_curl as hurl
>>> import json
>>> r = hurl.get("http://crowdprocess.no.de/"+keyname+"/results"",
... debug=custom_debug, auth=('username', 'password'),)
>>> consume(json.loads(r.content))
于 2011-11-22T07:50:53.190 に答える
0

私はon_receive(self, data): print somethingというあなたの郵便番号を見ることができませんでした。
それに追加sys.stderr.write("%s\n" % data)

def on_receive(self, data):
        # -- print data to stderr --
        import sys
        sys.stderr.write("%s\n" % data)
        # -- end --
        self.buffer += data  
        if data.endswith("\r\n") and self.buffer.strip():  
            content = json.loads(self.buffer)
            self.consume(content)
            self.buffer = ""
于 2013-10-30T21:59:12.287 に答える