[Twisted-Python] Re: Twisted protocol

Andrew Bennetts andrew-twisted at puzzling.org
Mon Mar 11 20:25:10 EST 2002

On Mon, Mar 11, 2002 at 06:51:57PM +0100, David wrote:
> Hello spiv, this is supposed to be reaching you =)
> I was told by radix on #python at opn that you had written "a little proxy 
> protocol which dispatches to multiple other protocol objects" in 
> twisted. This is exactly what i need for a project of mine.
> I'd very much appriciate if you would share this little piece of code 
> with me or alternativly explain how it works.

Heh.  I'm CC'ing the list with this, just for the sake of it :)

Here is the class.  I created it for a case where I had a protocol that was
sometimes inheriting from one Protocol, and sometimes another, and I didn't
want to diddle with __bases__ or munge the two bases into one class
(although perhaps that is the "better" solution?).  If you like, imagine
PacketBasedProtocolA/B to be basic.LineReceiver and Int32StringReceiver,
where lineReceived and stringReceived are both aliased to packetReceived.

class ProxiedPacketProtocol(Protocol):
    """General packet-based protocol.
    This class provides a simple way to use either PacketBasedProtocolA or
    PacketBasedProtocolB (or potentially others) as protocol for a data feed,
    without having to change which one you inherit from at runtime.  It
    basically acts as a proxy protocol between the low-level protocol and your

    Simply override parentProtocol with the "parent" protocol (the default is
    PacketBasedProtocolA) and then when packetReceived is triggered in the
    parent, packetReceived is called in me.  

    Inherit from this class, and override packetReceived."""
    parentProtocol = PacketBasedProtocolA
    packets = {}        # This is copied to the parent

    def __init__(self):
        # Create a private subclass of the parentProtocol so that we can
        # override its class-level data attribute(s).
        class ParentProtocol(self.parentProtocol):
            packets = self.packets

        # Create the parent protocol
        self.parent = ParentProtocol()

        # Raw data should get delivered to the parent...
        self.dataReceived = self.parent.dataReceived
        # ...and processed packets should come back to me
        self.parent.packetReceived = self.packetReceived
        self.parent.unknownPacket = self.unknownPacket

        # Data from the pinger needs to come via me
        self.parent.transport = self.transport

    def packetReceived(self, packet):
        """Override this.  
        Called whenever the parentProtocol receives a packet."""

    def unknownPacket(self, messageType, data):
        print "Received unknown packet of type '%s': %s" \
              % (messageType, repr(data))

David, I hope this is what you were after :)

It should be fairly easy to use and adapt -- but my question for the list is,
is it worth the extra effort it would take to make this more general?  i.e.
take arbitrary lists of attributes to copy from the parent to the proxy (and
vice versa?), instead of hard-coded names in __init__?

My other question is, is this a useful enough concept to include it in 
Twisted?  :)


More information about the Twisted-Python mailing list