2017-07-10 68 views
0

我在遠程服務器上有一個python程序。我需要創建一個網頁(html代碼與服務器上的python腳本存在於同一個目錄中),並且有一個按鈕用於點擊哪個python腳本應該運行。還有一件事是,我們需要從本地機器上選擇一個文件,然後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(
 
      { 
 
      \t \t type: "POST", 
 
      \t \t url: "./summarize.py", 
 
      \t \t 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新手,我無法遵循它們。有人解釋要做什麼。

謝謝。

回答

0

我想你可以使用php exec來運行Python和你的summarize.py腳本。

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

PHP exec將返回結果。你也可以使用django或其他框架來運行你的Python代碼。

相關問題