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

jkv jkv at unixcluster.dk
Thu Feb 26 17:33:39 EST 2009


Jean-Paul Calderone wrote:
>    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).
I am running this script inside a wrapper (honeyd), so i cant really use 
twisted since i don't have raw network access - i only got stdout, stdin 
and stderr to play with.

But i found a solution, posting it here for the archives:

password = ""
while 1:
   c = sys.stdin.read(1)
   if c == '\r' or c == '\n':
       break
   password = password + c



Regards,
jkv



More information about the Python-list mailing list