Re: Creating a hybrid server in Twisted

David Bolen <[email protected]>
Newsgroups gmane.comp.python.twisted.web,gmane.comp.python.twisted
Message-ID <[email protected]>
Glyph Lefkowitz <[email protected]> writes:

> On Apr 4, 2011, at 7:51 PM, David Bolen wrote:
>
>> Jashank Jeremy <[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.