Java 向正在运行的Python脚本发送命令
我正在开发一个小型java应用程序,它需要启动python脚本并与之交互。python脚本将在后台运行并等待命令。在每个命令之后,我期望得到一个响应,该响应将被转发回java应用程序 我已经使用了这些示例并打开了python脚本 我的问题是,如何在不重新运行python脚本钩子的情况下运行命令Java 向正在运行的Python脚本发送命令,java,python,Java,Python,我正在开发一个小型java应用程序,它需要启动python脚本并与之交互。python脚本将在后台运行并等待命令。在每个命令之后,我期望得到一个响应,该响应将被转发回java应用程序 我已经使用了这些示例并打开了python脚本 我的问题是,如何在不重新运行python脚本钩子的情况下运行命令 public void startProcess() { try { p = Runtime.getRuntime().exec("python " + scriptPath);
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编写的。这个脚本做了3件事。
WhoIs:获取通过bacnet连接的所有设备的列表
ReadHexFile:读取要发送到网络上所有设备的文本文件
SendFile:将文件发送到所有设备
我没有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脚本不断侦听新的输入或请求(来回通信),但我认为这会有点麻烦,而且还会使Python脚本不如标准的input->process->output
流清晰
避免运行多个Python脚本的原因是什么
要将输入写入脚本stdin,请执行以下操作:
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("python", "path\\to\\script.py");
Process pr = pb.start();
try (BufferedWriter writerToProc = new BufferedWriter(
new OutputStreamWriter(pr.getOutputStream()));
BufferedReader readerOfProc = new BufferedReader(
new InputStreamReader(pr.getInputStream()));
BufferedReader errorsOfProc = new BufferedReader(
new InputStreamReader(pr.getErrorStream()))) {
writerToProc.write("WhoIs\n");
writerToProc.write("ReadHexFile\n"); // is this the syntax?
writerToProc.write("SendFile 'path\to\file.txt'\n");
writerToProc.flush();
StringBuilder procOutput = new StringBuilder();
boolean gaveUp = false;
long waitTime = 10 * 1_000; // 10 seconds
long lastRead = System.currentTimeMillis();
for(;;) {
final long currTime = System.currentTimeMillis();
final int available = readerOfProc.available();
if(available > 0){
// TODO read the available bytes without blocking
byte[] bytes = new byte[available];
readerOfProc.read(bytes);
procOutput.append(new String(bytes));
// maybe check this input for an EOF code
// your python task should write EOF when it has finished
lastRead = currTime;
} else if((currTime - lastRead) > waitTime){
gaveUp = true;
break;
}
}
// readerOfProc.lines().forEach((l) -> System.out.println(l));
// errorsOfProc.lines().forEach((l) -> System.out.println(l));
}
}
我对python非常缺乏经验。python脚本大部分是给我的。我只是对脚本做了一些编辑以进行文件解析。这个脚本有三个功能。查找设备列表。加载到文本文件中。将文件发送到设备。所有这些数据都存储在脚本中,并且似乎不容易发送到多个脚本如果我错了,请更正我,但您最近的编辑将在每次通过HYES
Runtime.getRuntime().exec(新字符串[]{“python”)运行脚本的新实例,scriptPath,…
将运行该脚本的新实例。不幸的是,我无法以这种方式运行我的脚本。该脚本不需要参数启动,它只是在启动后等待参数。是的,明白了。我的另一条评论说,您需要写入进程标准输入流。请给我一分钟,我将编写代码来执行此操作。什么为python脚本提供参数?python脚本是否已经在运行时侦听输入,如果是,如何侦听?python脚本确实在等待输入。我不确定如何侦听,但这是它所基于的脚本。如果您不知道脚本侦听的是什么输入,那么您将无法与其通信。您需要首先找出通信方法,或者查找脚本上的文档,运行脚本以查看它是否表示需要参数或输入,或者阅读脚本并查找输入或侦听器。似乎hex文件可能是唯一的输入,因为它听起来像脚本自己检测网络上的设备并发送hex f在得到“whois”命令之前,脚本不会执行任何操作。如果不输入任何命令,脚本只会旋转