Article 5NP64 Unbuffered read from stdin in python

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.latest?d=yIl2AUoC8zA latest?i=Up02m2AZz7o:FLFrkxQzOtY:F7zBnMy latest?i=Up02m2AZz7o:FLFrkxQzOtY:V_sGLiP latest?d=qj6IDK7rITs latest?i=Up02m2AZz7o:FLFrkxQzOtY:gIN9vFwUp02m2AZz7o
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments