EOL for sys.stdin.readline() and raw_input()

MRAB google at mrabarnett.plus.com
Thu Feb 26 11:18:19 EST 2009


jkv 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?
> 
Could you insert some kind of filter into the input stream? The filter
would replace '\r\000' with '\r\n' (or just '\000' with '\n').

old_stdin = sys.stdin
try:
    sys.stdin = MyFilter(sys.stdin)
    ...
finally:
    sys.stdin = old_stdin

I don't know how many methods you would need to provide.



More information about the Python-list mailing list