私は、リターンコードが私が期待しているものかどうかを確認するGETリクエストを行いたいと思っています。この要求は、一般的な遅延チェーンのaddCallbackによって呼び出される関数内で発生します(下のコードで示されています)。他の内部で遅延されたHTTP要求を取得する方法(カスケード遅延)?
私の具体的な質問は:行の復帰方法-D-は行-E-に到着しますか?
コールバック関数 "cbResponse"(line -D-)が呼び出されないようです。私の最初の試みはリクエストを行い、コールバックチェーンにリクエストの結果を返すことでした(行-A-)。 deferrオブジェクトには属性の結果がないため、失敗します。 2番目のattemp(line -B-)は、遅延オブジェクト自体を返します。結果も返されません。 3つ目のattemp(-C-)は何も返しませんが、明らかに要求の応答コードはありません。
ありがとうございます!あなたは、正常に動作ドキュメントにリンクの例を見るために原子炉を実行している必要がTwisted Agentを使用している
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from twisted.internet import reactor, defer
class Test (object):
@classmethod
def getRequest (self, result):
print "Function getRequest"
agent = Agent(reactor)
d2 = agent.request('GET',
'http://www.google.com',
Headers({'User-Agent': ['Twisted Web Client Example']}),
None)
d2.addCallback(Test.cbResponse)
# 1 st attempt: return the result of d2. Fail: exceptions.AttributeError: Deferred instance has no attribute 'result'
return d2.result # --> line A
# 2nd attempt: return only the deferr object d2. Don't fail, but I can't get the result of the above request
### return d2 # --> line B
# 3rd attemp: return None (without return).
# --> line C
@classmethod
def cbResponse(response):
print 'Function cbResponse %s', response.code
# This is the return value I want to pass back to deferredChain function (called at line E)
return response.code # line --> D
@classmethod
def deferredChain(self):
d = defer.Deferred()
d.addCallback(Test.getRequest) # line --> E
d.callback("success")
return d.result # line --> F
if __name__ == '__main__':
tst = Test()
rtn = tst.deferredChain()
print "RTN: %s " % rtn
何が問題なのか、何が問題なのかがはっきりしないので、質問を言い換えてください。 – pinturic
申し訳ありません@pinturic、私は調整しました。今はどうか分かりますか? –