Multiple response for a single request in Twisted

1.9k Views Asked by At

I wanted to be able to receive multiple response from a server after sending one single request. This is implemented all in twisted. The Server:

class HandleReq(resource.Resource):
    def __init__(self):
        resource.Resource.__init__(self)

    def render_GET(self, request):
        """
          Here I basically connect to another server and get multiple
          responses"""
         d = defer.Deferred()
         interface = RemoteService(request, i_deferred)
         self._connect_to_RemoteService(bf_command, interface)
         self.handleCallbacks(i_deferred, request)
         return server.NOT_DONE_YET

    def render_POST(self, request):
        '''to make sure both GET/POST are handled'''
        return self.render_GET(request)

    def handleCallbacks(self, d, req):
        msg = d.addCallback(self.getEvent)
        d.addCallback(self.postResponse(req, msg))
        return None

    def getEvent(self, msg):
        return msg

    def postResponse(self, request, response):
        def post(event):
            request.setHeader('Content-Type', 'application/json')
            request.write(response)
            request.finish()
            self.postResponse(request, response)
            return server.NOT_DONE_YET
      return post

And the Client:

from urllib2 import URLError, HTTPError
api_req = 'http://localhost:8000/req' + '?' + urllib.urlencode({"request": request})
req = urllib2.Request(api_req)
try:
    response = urllib2.urlopen(api_req)

except HTTPError, e:
            print 'Problem with the request'
            print 'Error code: ', e.code
except URLError, e:
            print 'Reason: ', e.reason
else:
     j_response = json.loads(response.read())

Basically what I want is that the server not to close the connection (request.finish()), but instead to continue sending responses; and the client should be able to receive those messages.

1

There are 1 best solutions below

0
On

HTTP does not work this way. An HTTP request has exactly one response. Twisted Web will not let you send more than one response, because that would be against the HTTP specification and no HTTP clients would be able to figure out what was going on.

There may be another way to accomplish your underlying goals, but whatever it is, it won't involve sending more than one HTTP response to a single HTTP request.