需求:在浏览器上有一块python代码编辑块,编辑完后点击运行按钮,后台执行python代码将结果返回给前端展示。
用到的东西:前端用xterm.js 通信用websocket 后端用springboot
问题:如果执行过程中遇到input我要如何跟前端互动?
运行python文件的代码如下(遇到input直接卡了,在idea终端输入值也无效):
proc = Runtime.getRuntime().exec("python "+Path);// 执行py文件 //用输入输出流来截取结果 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"gb2312")); String line = null; while ((line = in.readLine()) != null) { //System.out.println(line); textArr.add(line); } BufferedReader errorin = new BufferedReader(new InputStreamReader(proc.getErrorStream(),"gb2312")); while ((line = errorin.readLine()) != null) { //System.out.println(line); errorTextArr.add(line); } in.close(); proc.waitFor();