3

I got a strange behavior in my Python code. It runs fine in my Windows console

For example,

@cmd.exe : python file.py

Content of my file.py file

print("-------------------------- RANDOM STRING HERE! --------------------------------")
email = input()
print("-------------------------- RANDOM STRING HERE! --------------------------------")
name = input()
print("-------------------------- RANDOM STRING HERE! --------------------------------")
address = input()
print("-------------------------- RANDOM STRING HERE! --------------------------------")
print(email+name+address)

This same code doesn't work when I do:

curl ://filepath/file.py | sudo python3

in an a console under SSH. I already tried with PuTTY and Git Bash, but I am still getting the same error.

EOFError in SSH Console:

EOFError in SSH Console

I already tried to use sys.stdin, but it doesn't work as expected.

2
  • You are loading the script over stdin. Python has already read it all and there is nothing more available. Commented Apr 15, 2017 at 22:45
  • @ephemient searched and understood in someway how stdin works.. i still can't get the user's input under those consoles.. tried sys.stdin.read() => nope and sys.stdin.buffer.raw.read(100), works in cmd but nope via ssh.. (unlimited field and doesn't submit when pressing enter) .. Is there any way expected to work? Commented Apr 15, 2017 at 23:44

1 Answer 1

2

No, really, you can't do that this way. Running

... | sudo python3

puts the script to the stdin so you can't use the stdin from that script any more.

But you can do it the other way round without a pipe using a temporary file:

curl ://filepath/file.py -o /tmp/script
sudo python3 /tmp/script

Or using process substitution (in Bash):

python3 <(curl ://filepath/file.py)
Sign up to request clarification or add additional context in comments.

2 Comments

Makes sense! Partially worked using process substituion (doesn't work with sudo) returns for some reason: python3: can't open file '/dev/fd/63': [Errno 2] No such file or directory and (23) Failed writing body, temporary file doesn't work either resulting curl: (6) Could not resolve host: sudo curl: (6) Could not resolve host: python3 curl: (3) <url> malformed -- syntax error? Upvoted and i lack some reputation, Thanks @Jakuje !
Yes. Obviously the sudo closes all the additional file descriptors (which is a good security practice, but breaks this use case). Without sudo it can work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.