LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2021, 04:56 PM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,612

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975

Quote:
Originally Posted by Bad Blue Bull View Post
What I need is unbuffered stdin stream.
Are you sure? I don't have a telnet server handy to test, but -u works for me in
Code:
(echo -n x; sleep 1; echo -n y; sleep 1; echo -n z) | python3 -u from866.py
works for me (whereas without the -u, there is no output for 2 seconds).
 
1 members found this post helpful.
Old 08-25-2021, 08:22 AM   #17
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,618

Rep: Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315

It's important to note that this is only the case with the second code example in post #1 - the first example is looping/splitting on newlines and since none are received it only prints once, when the input is complete.

(For it to work with both code examples, simply don't exclude newlines from the echo, i.e remove the -n args.)

Also, an important thing to check - the original code only says "python", which is often python2. (Even when it's not, it's better to be explicit and request python3.)

Thirdly, the in-code equivalent to the -u argument is "print(...,flush=True)", so given an executable from866.py of:

Code:
#!/usr/bin/env python3

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='',flush=True)
One can then use:
Code:
(for i in {1..5}; do echo "$i";sleep 1;done) | ./from866.py

If that isn't working with telnet, I would start by adding timestamps and seeing if that indicates whether the issue is on the Telnet side or the Python side.


Last edited by boughtonp; 08-25-2021 at 08:25 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Python can't read script from stdin when input() appears in the script. mimorek Programming 10 04-24-2021 08:14 AM
Sometimes, sed --unbuffered is not unbuffered kubuntu-man Programming 1 04-08-2016 12:16 PM
Python: Stdin and python apps fighting? enigma_0Z Programming 2 07-15-2008 08:45 AM
unbuffered stdin Mike Davies Linux - Software 1 03-03-2006 12:13 AM
unbuffered stdin mvt Programming 7 05-17-2004 09:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration