![](https://secure.gravatar.com/avatar/a61e243764490913906c773c9acb0d3c.jpg?s=120&d=mm&r=g)
Glyph Lefkowitz <glyph@twistedmatrix.com> writes:
On Apr 4, 2011, at 7:51 PM, David Bolen wrote:
Jashank Jeremy <jashank.jeremy@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