douju3911
douju3911
2018-06-29 17:37
浏览 176

Python获取发布数据以调用远程python脚本以显示在页面上

I am pretty new to python as I've been using PHP in the past. I am trying to practice python by converting my PHP code to python. I have the following php code to receive POST data from webserver and make a call over ssh to output a text file of a remote server back to the page.

app.js:

$(document).ready(function(){
$("button").on('click', function() {
    //call python script to generate report
     $.get("/", function(data){
        $( "#statusOutput" ).val(data);
    });
});
});

gettextoutput.php:

<?php //gettextoutput.php

    $user = 'user';
    $password = 'pass';
    $path = '/path/to/my/text/file';

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $hostname = $_POST['hostname']; //10.139.x.x
        $textoutput = file_get_contents("ftp://$user:$password@$hostname/$path");
        echo $textoutput; // I can use this to display the text output back to the page
    }
?>

I was wondering if there is a way to do this in python as well? Any information would be appreciated!

图片转代码服务由CSDN问答提供 功能建议

我是python的新手,因为我过去一直在使用PHP。 我试图通过将我的PHP代码转换为python来练习python。 我有以下PHP代码从Web服务器接收POST数据并通过ssh进行调用,以将远程服务器的文本文件输出回页面。

app.js:

 <代码> $(文件)。就绪(函数(){
 $的( “链接”)上。  ('click',function(){
 //调用python脚本生成报告
 $ .get(“/”,function(data){
 $(“#statusOutput”).val(data); \  n}); 
}); 
}); 
   
 
 

gettextoutput.php:

 &lt  ;?php //gettextoutput.php 
nn $ user ='user'; 
 $ password ='pass'; 
 $ path ='/ path / to / my / text / file'; 
 
  if($ _SERVER ['REQUEST_METHOD'] =='POST'){
 $ hostname = $ _POST ['hostname'];  //10.139.x.x
 $ textoutput = file_get_contents(“ftp:// $ user:$ password @ $ hostname / $ path”); 
 echo $ textoutput;  //我可以用它来显示文本输出回到页面
} 
?&gt; 
   
 
 

我想知道是否有办法做 这在python中也是如此? 任何信息将不胜感激!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douye2020
    douye2020 2018-06-29 17:56
    已采纳

    This should get you on the path to do it. using Flask and FTPlib that you must install. This works with a server named werkzeug (WSGI) out of the box included in Flask.

    #This answers makes a few assumptions | assumes a payload in json format | assumes Flask as framework | Assumes werkzeug as a WSGI server
    from Flask import Flask, request, send_file
    from ftplib import FTP                                                                      
    
    app = Flask(__name__)
    
    @app.route('/', methods['POST'])
    def get_some_file():
        input = request.get_json()
        ftp = FTP("SOMESERVERFTPIP")                                                                               
        ftp.login(input['user'],input['password'])
    #This will create local file and write contents of ftp file to it
        with open(/local/path/+input['path'], 'w') as f:
            ftp.retrbinary('RETR %s' % input['path'], f.write)
    
        #Filename should be a path, you may concatenate etc..
        return send_file('/local/path'/+input['filename'],
                         mimetype='text/txt',
                         attachment_filename='filename',
                         as_attachment=True)
    
    点赞 评论

相关推荐