EOL for sys.stdin.readline() and raw_input()
Jean-Paul Calderone
exarkun at divmod.com
Thu Feb 26 14:42:46 EST 2009
On Thu, 26 Feb 2009 11:46:24 +0100, jkv <jkv at unixcluster.dk> wrote:
>Hi,
>
>Are there any way to change the EOL character for sys.stdin.readline() and
>raw_input()?
>My problem is that i cannot rely on that the clients connection to my
>application will end all lines with \n or \r\n. Sometimes they will use
>\r\000 as EOL.
>
>Example from my code:
>
> sys.stdout.write('Password: ');
> #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01')
> password = raw_input()
>
>If the client sends 'qwerty\r\n' everything is fine and the password get
>stored in the variable. But sometimes, depending on how broken the telnet
>client are, the client will send 'qwerty\r\000' instead and then my
>application will hang at 'password = raw_input()' forever.
>
>Any suggestions?
It sounds like you might want to use a real implementation of the telnet
protocol in your application. Such an implementation would deal with the
fact that telnet encodes \r as \r\0 for you.
Twisted includes a telnet implementation, as well as facilities for reading
stdin asynchronously (which will let you avoid indefinite hangs).
Jean-Paul
More information about the Python-list
mailing list