我正在Apache网络服务器(也正在运行Flask)上开发一个Web应用程序,以将请求(按钮单击事件)发送到远程服务器,并等待返回以在网页上显示输出。我的远程服务器上有一个Python软件包已在工作(此软件包用于完成系统检查并在完成后创建报告),我只需要从远程模块中调用某些模块/功能即可获取信息以显示在我的网页上。目前,我能够与远程服务器对话,并且只需返回虚拟文本文件的输出即可。
来自PHP,我通常只运行ajax调用来调用我的php脚本,然后它将运行类似ssh2_exec($connection, $script)
我现在正在将PHP转换为python,所以我想知道是否可以通过Web服务器在远程服务器上调用python模块吗?
这是我到目前为止所拥有的:
app.py
import sys
from flask import Flask, request, send_file, render_template, json, make_response
from flask_cors import CORS, cross_origin
from ftplib import FTP
app = Flask(__name__)
CORS(app)
@app.route('/')
def hello_world():
return 'hello world!'
@app.route('/usb', methods=['GET', 'POST'])
def index():
return render_template('index.html')
@app.route('/get_file', methods=['GET', 'POST'])
def get_file():
ftp = FTP("10.139.X.X")
ftp.login()
#This will create local file and write contents of ftp file to it
with open('/local/path/to/report.log', 'wb') as f:
ftp.retrbinary("RETR /remote/path/to/reports.log", f.write)
ftp.close()
#Filename should be a path, you may concatenate etc..
return send_file('/local/path/to/report.log',
mimetype='text/txt',
attachment_filename='report.log',
as_attachment=True)
if __name__ == "__main__":
app.run(host= '0.0.0.0', debug=True)
app.js
$(function(){
$(".statusAreaDiv").find("button").click(function(){
startSpinner();
$.ajax({
url: 'http://hostname:5000/get_file',
type: 'GET',
success: function(response){
$( ".textArea" ).val(response);
},
error: function(error){
console.log("Here is the error res: " + JSON.stringify(error));
}
});
});