скрученный Получить данные ответа на клиенте

Я следовал этому руководству, но не знаю, как получить данные ответа от сервера.

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

Я знаю, что данные ответа будут отображаться в клиенте

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

Как я могу получить возвращенное сообщение с сервера и сохранить его в переменной?


person TomNg    schedule 15.08.2014    source источник


Ответы (1)


Просто заставьте dataReceived хранить display в переменной экземпляра и добавлять к ней каждый раз, когда вызывается dataReceived. Затем, когда вызывается 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)

В контексте полного примера:

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()
person dano    schedule 15.08.2014
comment
Спасибо, дано, но что, если я хочу сохранить данные в переменной, которая находится за пределами класса BeginningPrinter? Я имею в виду, что данные, которые вы получаете, все еще находятся внутри этого класса. - person TomNg; 16.08.2014
comment
@coderkisser Вот для чего можно использовать finished Deferred. Я расширил свой ответ, чтобы продемонстрировать. - person dano; 16.08.2014
comment
Я все еще не могу получить данные в переменной, потому что вы передаете их функции. Мне нужно что-то вроде: message = handle_result(). Пожалуйста, помогите. - person TomNg; 18.08.2014
comment
@coderkisser Зачем тебе это нужно? Программы, написанные с помощью Twisted, организованы иначе, чем обычные программы. Извращенный способ сделать что-то с ответом — зарегистрировать обратный вызов. По-другому делать не имеет смысла, так как вся программа выполняется внутри реактора. - person dano; 18.08.2014
comment
Хорошо я понял. Спасибо еще раз. - person TomNg; 18.08.2014