[Twisted-web] Creating a hybrid server in Twisted

David Bolen db3l.net at gmail.com
Wed Apr 6 13:31:44 EDT 2011

Glyph Lefkowitz <glyph at twistedmatrix.com> writes:

> On Apr 4, 2011, at 7:51 PM, David Bolen wrote:
>> Jashank Jeremy <jashank.jeremy at optusnet.com.au> writes:
>>> So I'm pretty much stuck in a rut.  I don't want to totally reinvent the
>>> wheel just to be able to protocol-switch; I'd prefer to make use of
>>> existing code from Twisted.  How do I hijack Twisted.Web to add protocol
>>> switching?
>> To insert your own protocol code, you want to set the "protocol"
>> attribute of your Site (or HTTPFactory if using that directly)
>> instance to your own "hybrid" protocol class.
> It would be slightly better to override buildProtocol, because that
> way you can call the superclass's implementation more idiomatically.
> But, the idea is the same.

Good point.  For myself, I guess I use the attribute patching in cases
where I subclass an existing protocol, but won't be changing the
initialization.  That way I didn't need to keep an independent factory
class in sync with the library version over time in terms of any
buildProtocol processing, which feels more robust.

But a custom factory with its own buildProtocol is definitely more
flexible, not to mention required if the new protocol class needs a
different initialization signature.

-- David

More information about the Twisted-web mailing list