在嵌入式Tomcat上从Spring-Boot Web应用程序运行Python脚本 [英] Running a Python script from Spring-Boot Web Application on embedded Tomcat

查看:305
本文介绍了在嵌入式Tomcat上从Spring-Boot Web应用程序运行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个Web应用程序,该应用程序在Spring-boot上使用HTML Form来接受用户的输入(这在嵌入式Tomcat上运行);我需要将此输入作为命令行参数传递给位于硬盘上某个地方的Python脚本.

I am trying to make a web-app that takes input from the user using HTML Form on Spring-boot (Which is running on embedded Tomcat); I need to pass this input to a Python script sitting somewhere on my hard disk as command-line arg.

这是我的控制器,可从HTML表单中获取数据(从表单中获取数据工作正常.)

Here is my controller that fetches the data from HTML form (Getting data from the form is working fine.)

@Controller
public class PortalController {
    @RequestMapping(value="")
    public String hello(){
        return "portal/welcome";
    }
    @RequestMapping(value="",method=RequestMethod.POST)
    public String addAObjectForm(@RequestParam String val) throws IOException {
        String cmd = "python Fetch.py \""+val+"\"";
        System.out.println(cmd);
        Runtime.getRuntime().exec(cmd);
        return "redirect:/";
    }
}

变量"cmd"最终存储该值:python Fetch.py "Command-line-arg-from-html"

The variable 'cmd' stores this value finally: python Fetch.py "Command-line-arg-from-html"

Python代码正在抓取网络. 代码太大,无法移植到Java.而且我的v.sweet老师要求我在Spring-Boot上制作该网络应用.由于该应用在Tomcat上运行,因此我似乎无法像直接执行Py脚本那样执行该操作.它在控制台上.有人可以建议一种方法吗?任何帮助都将受到高度赞赏.

The Python code is scraping web. The code is so big that it cannot be ported to Java. And my v. sweet teacher has asked me to make the web-app on Spring-Boot. Since the app is running on Tomcat, it doesn't look like I will be able to execute the Py script directly as if it were on console. Can someone suggest a way of doing it, please? Any bit of help is highly appreciated.

注意:Spring-Boot Project和Python Script都可以独立运行而没有问题.

我遇到的问题是,代码仅在控制台上打印变量"cmd"并继续.数据库中没有反映出任何更改(应该由Py脚本完成),这表明该脚本未执行. 这是控制台窗口:

The issue I am having is, the code just prints the variable 'cmd' on the console and continues. No changes are reflected in the database (which are supposed to be done by the Py script), it indicates that the script did not execute. Here is the console window:

2018-03-03 05:44:47.733  INFO 10752 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-03 05:44:47.771  INFO 10752 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-03-03 05:44:47.771  INFO 10752 --- [  restartedMain] org.OpenLyrics.Portal.PortalApplication  : Started PortalApplication in 3.161 seconds (JVM running for 7.414)
2018-03-03 05:44:55.737  INFO 10752 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-03-03 05:44:55.738  INFO 10752 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-03-03 05:44:55.765  INFO 10752 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 26 ms
python Fetch.py "Argument 1"
python Fetch.py "Argument 2"
python Fetch.py "Argument 3"

推荐答案

我在互联网上挖了很多东西,找到了解决方法.

I dug the internet a lot and found the solution.

可以使用以下命令执行此操作:

One can do this using the following commands:

String fetching = "python " + "c:\\Fetch.py \"" + songDetails + "\"";
String[] commandToExecute = new String[]{"cmd.exe", "/c", fetching};
Runtime.getRuntime().exec(commandToExecute);

这篇关于在嵌入式Tomcat上从Spring-Boot Web应用程序运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆