[Twisted-Python] Telnet MCCP Transport

Vermeersch Simon simonvermeersch at gmail.com
Sat Sep 4 09:19:47 EDT 2010

Hello everyone,

I couldn't find an implementation of MCCP (Mud Client Compression
Protocol) using Twisted so I made my own, which I'm sharing if anyone
else has a use for it.

You can find a description of MCCP here:

Since I'm fairly new to Twisted I would appreciate any input about how
to improve this if you find a better/cleaner way to do it.

import zlib
from twisted.conch.telnet import TelnetTransport
from twisted.conch.telnet import DO, DONT

COMPRESS2 = chr(86)

class MCCPTelnetTransport(TelnetTransport):
    def connectionMade(self):
        self.zlib = None

    def commandReceived(self, command, argument):
        if argument == COMPRESS2:
            if command == DO:
                self.requestNegotiation(COMPRESS2, "")
                self.zlib = zlib.compressobj()
            elif command == DONT:
            TelnetTransport.commandReceived(self, command, argument)

    def write(self, data):
        data = data.replace('\n', '\r\n')
        if self.zlib:
            data = self.zlib.compress(data)
            data += self.zlib.flush(zlib.Z_SYNC_FLUSH)

Simon Vermeersch

More information about the Twisted-Python mailing list