Unbuffered read from stdin in python
by Bad Blue Bull from LinuxQuestions.org on (#5NP64)
I wanna convert telnet output from CP866 to UTF-8, I'm sure it's possible to do by redirecting output because telnet|cat is fine and also redirecting telnet output to file and reading the file with tail -f.
This is the script I tried:
Code:#!/usr/bin/env python
import io
import sys
input_stream = io.TextIOWrapper(
sys.stdin.buffer, encoding='cp866')
for line in input_stream:
print(line)same result with this:
Code:#!/usr/bin/env python
import io
import sys
input_stream = io.TextIOWrapper(
sys.stdin.buffer, encoding='cp866', newline='\n')
while True:
x = input_stream.read(1)
if len(x) == 0:
break
print(x, end='')Save it as from866, make it executable and try telnet <target> | from866.
It works fine when output of ls for example is redirected into it, but with telnet it prints all well until login prompt, but then the later stuff is terribly buffered. Login prompt appears only I input login. I looked at gnu cat source, don't see how it manages to get unbuffered input.
This is the script I tried:
Code:#!/usr/bin/env python
import io
import sys
input_stream = io.TextIOWrapper(
sys.stdin.buffer, encoding='cp866')
for line in input_stream:
print(line)same result with this:
Code:#!/usr/bin/env python
import io
import sys
input_stream = io.TextIOWrapper(
sys.stdin.buffer, encoding='cp866', newline='\n')
while True:
x = input_stream.read(1)
if len(x) == 0:
break
print(x, end='')Save it as from866, make it executable and try telnet <target> | from866.
It works fine when output of ls for example is redirected into it, but with telnet it prints all well until login prompt, but then the later stuff is terribly buffered. Login prompt appears only I input login. I looked at gnu cat source, don't see how it manages to get unbuffered input.