I'm writing a chat program in Python that needs to connect to a server before user input from sys.stdin is accepted. If a connection cannot be made then the program exits.
Running this from the shell, if a connection fails and input was sent while attempting to connect, the input is echoed to the shell after the program exits:
jtink@gab-dec:~$ python chat.py
attempting to connect...
Hey there! # This is user input
How are you? # This is more user input
connection failed
jtink@gab-dec:~$ Hey there!
Hey there!: command not found
jtink@gab-dev:~$ How are you?
How are you?: command not found
jtink@gab-dev:~$
Is there a way to tell if there is input left on sys.stdin, so that I can read it before the chat program exits?
I have read this similar question, but the answers only describe how to tell if input is being piped to a python program, not how to tell if there is input.