[Twisted-web] Telnet server: Detecting CTRL-<key>, as happends with CTRL-C

Jean-Paul Calderone exarkun at divmod.com
Wed Feb 4 10:15:48 EST 2009

On Wed, 4 Feb 2009 13:35:23 +0200, "Eloranta, Teuvo \(NSN - FI/Tampere\)" <teuvo.eloranta at nsn.com> wrote:
>I have an application, with Python 2.4 and Twisted 8.2.0.
>Can I somehow detect e.g. CTRL-J key presses in:
>  class Telnet_simulation(telnet.Telnet)
>	-->
>      def dataReceived(self, data):
>(or elsewhere in code)?

If the `telnet´ above is twisted.protocols.telnet, then you should switch
to twisted.conch.telnet.  If it's twisted.conch.telnet, then you should
override `applicationDataReceived´, not `dataReceived´ (or use one of the
other mechanisms for hooking into it - subclass and override is one
approach, but there are others which you can find examples of in doc/conch/

>When sending CTRL-C, the dataReceived() is called immeaditely, but when
>sending some other combinations, the data can be seen only after Enter
>is pressed?
>(telnet_Command is called then) But the CTRL-J is not a telnet command,
>it should act same way as CTRL-C.

Ah, telnet_Command, I guess you are using twisted.protocols.telnet. :)

Most likely your telnet client is in line editing mode.  This means it
isn't sending data typed in until enter is pressed, so of course you can't
receive it until then.  There are telnet negotiation options which you can
use from the server to ask the client to go into character mode.  You can
see some code which negotiates about linemode in twisted/conch/telnet.py
(search for LINEMODE).  I'm not sure if LINEMODE is the preferred option
for controlling this behavior (I seem to recall there being another one,
but I forget which obsoleted which).

Also, twisted-python at twistedmatrix.com might be a more appropriate list for
this topic, since this is a web-oriented list.


More information about the Twisted-web mailing list