我正在开发一个需要启动python脚本并与之交互的小java应用程序。python脚本将在后台运行并等待命令。在每个命令之后,我期望得到一个响应,它将被转发回java应用程序。在
如果没有python的话,我的脚本是如何运行的?在
public void startProcess()
{
try {
p = Runtime.getRuntime().exec("python " + scriptPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public String executeCommand(String cmd)
{
String consoleResponse = "";
try {
// how do I perform something similar to p.exec(cmd)
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((consoleResponse += stdInput.readLine()) != null) {
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((consoleResponse = stdError.readLine()) != null) {
}
} catch (IOException e) {
e.printStackTrace();
}
return consoleResponse;
}
编辑:python脚本用于BACpypes。剧本做了三件事。 WhoIs:获取通过bacnet连接的所有设备的列表 ReadHexFile:读取要发送到网络上所有设备的文本文件 SendFile:将文件发送到所有设备。在
我没有使用python的经验,我觉得将所有这些数据保存在一个脚本中会更简单。在
我想一个选择是将每个命令分解成自己的脚本,并将数据传递给java应用程序。在
您需要让单个Python脚本一直监听新的输入或请求(来回通信),但我认为这会有点麻烦,而且还会使Python脚本比标准的
input -> process -> output
流更不清晰。在避免运行多个Python脚本的原因是什么?在
要将输入写入脚本stdin,请执行以下操作:
相关问题 更多 >
编程相关推荐