代码之家  ›  专栏  ›  技术社区  ›  TomNg

twisted在客户端获取响应数据

  •  2
  • TomNg  · 技术社区  · 10 年前

    我跟踪了这个 tutorial 但我不知道如何从服务器获取响应数据。

    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)
    

    如何从服务器获取返回的消息并将其存储到变量中?

    1 回复  |  直到 10 年前
        1
  •  2
  •   dano    10 年前

    就这样 dataReceived 百货商店 display 在实例变量中,并每次附加到它 数据已接收 被调用。然后,一次 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()
    
    推荐文章