首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向正在运行的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
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51540597

复制
相关文章

相似问题

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