[英]How can I run a local python script on a remote host and communicate with it using subprocess?
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. 我正在尝试编写一个Python程序,该程序在远程主机上执行本地 Python脚本,并让我在脚本运行时与其通信。
My code currently consists of the two files run.py
and remote.py
: 我的代码当前包含两个文件run.py
和remote.py
:
run.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
: 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? 有什么办法可以将python脚本通过这种方式传递到远程Python解释器中,并且以后可以在该脚本上使用诸如input()
类的东西吗? How could I change my example to work? 我该如何改变自己的榜样呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.