首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >向正在运行的Python脚本发送命令

向正在运行的Python脚本发送命令
EN

Stack Overflow用户
提问于 2018-07-26 21:55:27
回答 1查看 781关注 0票数 0

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

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

我的问题是,如何在不重新运行python脚本钩子的情况下运行我的命令?

代码语言:javascript
复制
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件事。SendFile:获取通过be连接的所有设备的列表。ReadHexFile:读取要发送到网络上所有设备的文本文件。WhoIs:将文件发送到所有设备。

我没有使用python的经验,我觉得在一个脚本中保存所有这些数据会更简单。

我认为一种选择是将每个命令分解成其自己的脚本,并将数据传递给java应用程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-26 22:19:28

如何在不重新运行python脚本钩子的情况下运行我的命令?

您需要让单个Python脚本不断侦听新的输入或请求(来回通信),但我认为这将是一种轻微的痛苦,而且还会使您的python脚本不像标准的input -> process -> output流那样清晰。

避免运行多个Python脚本的原因是什么?

要将输入写入脚本stdin,请执行以下操作:

代码语言:javascript
复制
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));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51540597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档