EOFError when input() after print() in SSH console - python

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:
I already tried to use sys.stdin, but it doesn't work as expected.

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)

Related

How do I restart nginx from a python script?

I can use "nginx -s reload" command to restart nginx on the shell.
But, when I use os.system("nginx -s reload") command, It appears error.
/usr/local/bin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
For this error. I already install pcre. Is there some magic problems.
For running such commands in python scripts it's better to use subprocess library.
try this code instead of yours:
import subprocess
subprocess.call('whatever command you want to run it in terminal', shell=True)
Be lucky
hello I recommend that you first send this validation before sending the reset so you avoid headaches
reinicioNGINX = subprocess.getoutput(['nginx -t'])
if 'nginx: the configuration file /etc/nginx/nginx.conf syntax is ok' in reinicioNGINX:
_command_restart
else:
_command_avoid_restart

Vim commands running in bash after I run a python file

I am having an issue after I run a python unit test file. Once the file exits I can only interact with my console after pressing "i" and using other vim keybindings. I also noticed that using the arrow keys to traverse what I typed will delete a random number of characters at the end of the line.
EX:
$ ./tests.py -v
<output>
$ <cannot type>
<press "i">
$ I can now type
<press <- >
$ I can no
I am using RHEL 7 and bash. I've tried googling this issue but I'm either formatting the question poorly or it is an uncommon issue.
Thank you for the help.
EDIT:
The actual test.py contains private code, but this is example contains the same essential code.
test.py
#!/usr/bin/env python
import unittest
class TestUtil(unittest.TestCase):
def test_hello_world(self):
text = "Hello World!"
self.assertEqual("Hello World!", text)
print(text)
pass
if __name__ == '__main__':
unittest.main()
It sounds as if your shell is being placed into vi-mode. This is a readline mode where you can use vi editing keys instead of the more commonly used emacs keys.
There are two ways I know of that this can happen.
set -o vi
bindkey -v
Technically, to turn it off you use set +o vi. However, that will disable all inline editing. It is more likely that you wish to go back to emacs mode, which is usually the default. To do that, do this instead:
set -o emacs

"python -i print_content.py < file.txt" exits interactive mode

python -i <script.py> is theoretically meant to run script.py and proceed into interactive mode, however:
shell$ python -i print_content.py < file.txt
hello world
>>>
shell$
The script runs successfully and is followed by printing >>>, implying that the Python shell is entered following script execution.
I know I could bypass the issue by opening the file from within the script - I'm more curious to know the cause of the issue.
Note: Contents of print_content.py seem innocent enough:
import sys
for i in sys.stdin:
print i
Note: I'm using Python 2.7.3

Issue with running sudo command with python on plumbum

I'm using Python 2.7 with the latest plumbum package from mac ports.
In general, plumbum works great. Though I'm having a heck of a time getting a sudo'd command to work. I've setup my /etc/sudoers for the commands I want to run without having to be prompted, so that's fine. I can run the commands manually without issue.
However, when I try the same from python using this:
sudo["/usr/local/some-magic-command here"]
sudo("-u " + sudoUser) # sudo user is userfoo
I receive the following error:
plumbum.commands.processes.ProcessExecutionError: Command line: ['/usr/bin/sudo', '-u userfoo']
Exit code: 1
Stderr: | sudo: unknown user: userfoo
The user does exist, so not exactly sure what the deal is here.
Comments?
There is no "-u userfoo" user. There is probably just "userfoo". Note: no -u prefix. Try:
from plumbum.cmd import sudo
as_userfoo = sudo["-u", sudo_user]
print(as_userfoo("whoami"))

python sys library command prompt

I am trying out the sys library in python. In command prompt I am using this.
>>>import sys
>>>sys.ps1 ='$'
#my own input 'print 'test print''
$print 'test print'
test print
That worked in CLI, however, when I tried to do it in a python file and run it in CLI ( python file.py did not return anything.
#!/usr/bin/python
import sys
sys.ps1= '$'
Am I missing anything in the file?
What are you trying to do? sys.ps1 will just set the prompt for the python interpreter, so as soon as it exits it's effect is gone again. When running a file, there is no command-line.
If you want to see this have an effect, try running this with python -i file.py - this will run the command, and then drop you into the interactive shell, which should have PS1 set to $ now

Categories