【发布时间】:2022-01-30 03:49:27
【问题描述】:
大家好:我有一个基于 Fabric 的程序正在运行,它的工作是在远程服务器上上传、编译和运行一个 c 程序 (test.c)。对于上传,我只是使用 WinSCP 命令行工具和子进程模块。
我应该注意到我正在 PyCharm 中测试这个程序。
对于编译和运行,我使用的是 Fabric 连接。它可以工作,除非我的程序中有某种输入。例如 test.c 是:
#include <stdio.h>
int main(void)
{
printf("This is running on the server It is a new file..\n\nEnter a character: ");
int i;
scanf("%d", &i);
printf("It worked!. Your number is ... %d!\n", i);
}
这里是相关的python代码。
import sys
import subprocess
from fabric import Connection
from json import loads
info = loads(open('pwd.json').read())
def run():
# Upload the file. [Omitted]
print('Uploaded file!')
c = Connection(host='glue.umd.edu', user=info['username'],
connect_kwargs={'password': info['password']})
with c.cd('python_environment'):
result = c.run(f'gcc {filename} -Wall')
print(result.stdout)
print('Compiled! Running...')
result = c.run(f'a.out', echo=True, echo_stdin=True, pty=True)
print("Closing...")
c.close()
print("Completed!")
if __name__ == '__main__':
filename = 'test.c'
if 'run' in sys.argv:
run()
当然还有结果。
test.c: In function 'main':
test.c:9:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Compiled! Running...
cd python_environment && a.out
This is running on the server It is a new file..
Enter a character:
输入一个数字然后输入不起作用。我已经使用 PuTTY 在服务器上手动运行了该程序,它确实按预期工作。输入一个数字,然后“回车”将在服务器上打印出该数字。
【问题讨论】:
标签: python ssh user-input fabric