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

Phil Mayers p.mayers at imperial.ac.uk
Tue Oct 30 05:04:02 EDT 2012

On 10/30/2012 06:50 AM, Laurens Van Houtven wrote:
> On Mon, Oct 29, 2012 at 4:02 PM, <exarkun at twistedmatrix.com
> <mailto:exarkun at twistedmatrix.com>> wrote:
>     In case anyone didn't read #3204: this is, of course, an unsupported use
>     of transports and not guaranteed to work or continue working (currently
>     it will not work if you happen to be using the (preferred) protocol-
>     based SSL implementation).
> So, is there a non-terrible way of doing "feed the raw HTTP bytes that
> caused this request to this factory instead", in an IResource?

Would it not make more sense to subclass Site and swap out the Channel 
implementation for one that can be "pointed" somewhere else? It means 
you have to use that "Site" for any WebSocket enabled site, but it would 
be clean otherwise. Like so:

class WebSockCapableChannel(http.HTTPChannel):
   def __init__(self):
     self.websocket = False

   def startWebSocket(self, proto):
     self.websocket = True
     self.websocket_proto = proto
     proto.transport = someWrapper(self)

   def rawDataReceived(self, data):
     if self.websocket:
       http.HTTPChannel.rawDataReceived(self, data)

class WebSocketSite(server.Site):
   procol = WebSockCapableChannel

...then in your "Resource" do:

class MyResource(...):
   def render(...):

More information about the Twisted-Python mailing list