【问题标题】:How to run a python script on remote server by button click on web page?如何通过单击网页的按钮在远程服务器上运行 python 脚本?
【发布时间】:2017-07-10 10:47:25
【问题描述】:

我在远程服务器上有一个 python 程序。我需要创建一个网页(与服务器上的 python 脚本位于同一目录中的 html 代码)有一个按钮来单击应该运行的 python 脚本。还有一件事是我们需要从本地机器中选择一个文件,然后python脚本将该文件作为输入,运行并输出另一个需要在网页上显示的文件。

我不知道用什么 javascript 或 ajax 或 php 来实现这一点。我尝试了不同的方法但徒劳无功。

这是我一直在尝试的 html 代码...

<!DOCTYPE html>
  <html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

      <script>
       $(document).ready(function(){
        $("button").click(function(){

         // var file = $('#fileInput')[0].files[0]
         var fd = new FormData();
         fd.append('fileInput', file);
         // console.log(fd)
         $.ajax(
          {
          	  	type: "POST",
          	  	url: "./summarize.py",
          	  	data: "fd"
                success: function (response) {
                  
                }
              // error: function (response) {}
          });
       });
      });

      </script>
   </head>
    
   <body>
      <h3>Upload reviews file</h3>
      <input type="file" id="fileInput" name="fileInput">
      <p>Click the "Get summary" button to summarize the reviews.</p>
      <button>Get summary</button>
   </body> 

</html>

我在网上搜索过,但没有具体的答案(我觉得是这样)。由于我是 javascript 新手,因此在关注它们时遇到了麻烦。有人请解释要做什么。

谢谢。

【问题讨论】:

    标签: javascript php jquery python html


    【解决方案1】:

    我认为您可以使用 php exec 来运行 Python 和您的 summarize.py 脚本。

    <?php
    // @result array
    exec("python summarize.py", $result);
    

    PHP exec 将返回结果。您也可以使用 django 或其他框架来运行您的 Python 代码。

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      相关资源
      最近更新 更多