发送命令到运行Python脚本

2024-09-26 22:12:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个需要启动python脚本并与之交互的小java应用程序。python脚本将在后台运行并等待命令。在每个命令之后,我期望得到一个响应,它将被转发回java应用程序。在

我使用了示例herehere打开python脚本。在

如果没有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应用程序。在


Tags: the命令脚本应用程序newstringherejava
1条回答
网友
1楼 · 发布于 2024-09-26 22:12:27

how do I, without re-running the python script hook into it and run my commands?

您需要让单个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));
    }
}

相关问题 更多 >

    热门问题