[Twisted-Python] using stdio with Twisted
exarkun at divmod.com
Tue Mar 20 07:13:55 EDT 2007
On Mon, 19 Mar 2007 19:16:23 -0700, Neurophyre <listbox at evernex.com> wrote:
>I'm building an old-school BBS client using Twisted. It needs to take user
>input, parse it, send messages over the wire and get messages back and
>display them.. the usual. I'm just now getting to writing the UI and have
>run into a couple problems.
>First, whether using protocol.Protocol or protocols.basic.LineReceiver (in
>raw mode) with internet.stdio.StandardIO, I have the same problem: the
>dataReceived (or rawDataReceived in the case of LineReceiver in raw mode)
>functions are only called after one types some text and hits return. I
>need to process input on a keystroke level, since there will be prompts at
>which a single keystroke will do things.
>Second, I was looking at twisted.conch.insults.insults.TerminalProtocol
>after some Googling -- does anyone know if I can just drop this in in place
>of protocols.basic.LineReceiver (or whatever) and have it work properly,
>or does it require some special invocation and/or grab stdio by itself?
Take a look at what twisted.conch.stdio.runWithProtocol does to the terminal.
That's more or less what's necessary to kick it out of line buffered mode and
give you each keystroke as it arrives.
TerminalProtocol is meant to be used with ServerProtocol from the same module.
ServerProtocol will interpret various terminal control sequences and translate
them into calls onto whatever ITerminalProtocol you supply to it. It probably
won't be much use to use LineReceiver, since keystrokeReceived will be called
with things like backspace and home and other things LineReceiver has no idea
what to do with. There are a couple other things you might be interested in
though, like twisted.conch.recvline.RecvLine or a class in a project I've been
working on a bit on and off, invective.widgets.LineInputWidget, which uses
insults' widget system to implement a more re-usable line input class (you can
find invective in my Twisted sandbox,
), which will hopefully move back into Twisted at some point.
More information about the Twisted-Python