简体   繁体   English

在客户端获取响应数据

[英]twisted Get response data at client

I followed this tutorial but I don't know how to get response data from server. 我遵循了本教程,但是我不知道如何从服务器获取响应数据。

class Service(Resource):
    def render_POST(self, request):
        return 'response message'

I know that the response data will be displayed in the client 我知道响应数据将显示在客户端中

 def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            print 'Some data received:'
            print display
            self.remaining -= len(display)

How can I get the returned message from server and store it into a variable? 如何从服务器获取返回的消息并将其存储到变量中?

Just make dataReceived store display in an instance variable, and append to it every time dataReceived is called. 只需使dataReceived存储在实例变量中display ,并在每次调用dataReceived附加到该变量即可。 Then, once connectionLost is called, you know you have the complete response. 然后,一旦调用connectionLost ,您就知道您已完成响应。

class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.remaining = 1024 * 10
        self.total_response = ""  # This will store the response.

    def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            self.total_response += display  # Append to our response.
            self.remaining -= len(display)

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        print 'response is ',self.total_response
        self.finished.callback(self.total_response)

In the context of the full example: 在完整示例的上下文中:

from pprint import pformat

from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.remaining = 1024 * 10
        self.total_response = ""  # This will store the response.

    def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            self.total_response += display  # Append to our response.
            self.remaining -= len(display)

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        print 'response is ',self.total_response
        self.finished.callback(self.total_response)  # Executes all registered callbacks

def handle_result(response):
    print("Got response {}".format(response)

agent = Agent(reactor)
d = agent.request(
    'GET',
    'http://example.com/',
    Headers({'User-Agent': ['Twisted Web Client Example']}),
    None)

def cbRequest(response):
    print 'Response version:', response.version
    print 'Response code:', response.code
    print 'Response phrase:', response.phrase
    print 'Response headers:'
    print pformat(list(response.headers.getAllRawHeaders()))
    finished = Deferred()
    finished.addCallback(handle_result)  # handle_result will be called when the response is ready
    response.deliverBody(BeginningPrinter(finished))
    return finished
d.addCallback(cbRequest)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM