[Twisted-Python] Transplating a request's transport (SockJS)

Laurens Van Houtven _ at lvh.cc
Sun Oct 28 11:17:53 EDT 2012


I've been trying to play with SockJS + Twisted. There's an implementation
here: https://github.com/Fugiman/sockjs-twisted

However, I'm already running a web server, and as the README says
sockjs-twisted is *not* intended to be run together with a web server. It
doesn't really use any of the mechanics in twisted.web.

So, I tried to make it work using Resources anyway: reconstructing the
equivalent bytes in render, and replaying them with the SockJSFactory:

class SockJSResource(resource.Resource):
    A resource that defers to a SockJS factory.
    isLeaf = True

    def __init__(self, factory, options=None):
        self._factory = SockJSFactory(factory, options)

    def render(self, request):
        transport, request.transport = request.transport, None
        protocol = self._factory.buildProtocol(transport.getPeer())

        path = "/".join([""] + request.postpath)
        lines = ["{0} {1} HTTP/1.1".format(request.method, path)]
        for name, values in request.requestHeaders.getAllRawHeaders():
            lines.append("{0}: {1}".format(name, ",".join(values)))
        lines += ["", request.content.read()]

        data = "\r\n".join(lines)

        return server.NOT_DONE_YET

This only kind of works. There's an external sockjs test suite (more like
an acceptance test suite): https://github.com/sockjs/sockjs-protocol, that
has multiple failing tests. If anyone needs help running these, I'll gladly
assist there.

In an attempt to begin debugging this, I've found that basically none of
the protocol methods get called on that protocol instance I make on the
third line of render. This took me a while to figure out because
dataReceived *was* being called -- except then I realized I'm calling it
myself in that render method :)

This leads me to believe I'm essentially just screwing up transplanting
this transport entirely. Is that the case?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20121028/fb53c298/attachment-0001.htm 

More information about the Twisted-Python mailing list