I'm trying to write a Python program that executes a local Python script on a remote host and lets me communicate with the script when it's running.
My code currently consists of the two files run.py
and remote.py
:
run.py
:
import os
import subprocess as sp
with open(os.path.join(os.path.dirname(__file__), "remote.py")) as scriptf:
node_script = scriptf.read()
sess = sp.Popen(["ssh", "remote_host", "/usr/bin/python -"], stdin=sp.PIPE, stdout=sp.PIPE)
stdout, stderr = sess.communicate(node_script)
print(stdout, stderr)
sess.stdin.write("Print me this!\n")
remote.py
:
import sys
print("Hello!")
while True:
line = input()
print(line)
This gives me the following error:
$ python run.py
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
EOFError: EOF when reading a line
('Hello!\n', None)
Traceback (most recent call last):
File "run.py", line 10, in <module>
sess.stdin.write("Print me this!\n")
ValueError: I/O operation on closed file
Is there any way I can pipe a python script into a remote Python interpreter like this AND be able to use something like input()
on that script later? How could I change my example to work?