在Ajax调用中调用Python文件

时间:2021-02-16 20:27:52

So I have established a pretty decent understanding of the simple architecture of an angularjs app, calling $http and posting to a php page, and receiving data back.

因此,我对angularjs应用程序的简单架构有了相当好的理解,调用了$http并将其发布到php页面,并接收数据。

What I'm wondering, is how to do the same type of function with python. Is it possible to have python act the same, with self contained script files that accept post data and echo json back?

我想知道的是,如何使用python实现相同类型的函数。是否有可能让python的行为相同,使用自包含的脚本文件来接受post数据和echo json ?

$username = $_POST['username'];

type variable assignment at the beginning of the script, and:

在脚本开头键入变量赋值,并且:

echo json_encode(response);

type response.

类型的反应。

I'm wanting to use Python for some Internal Tools for my company, as it offers better libraries for remotely running powershell scripts (as the tools are all linux hosted) and overall just has libraries that fit my needs. I'm just having a difficult time finding a concise answer to how this could be set up.

我希望将Python用于我的公司的一些内部工具,因为它为远程运行powershell脚本提供了更好的库(因为这些工具都是linux托管的),而且总体上只有适合我需要的库。我只是很难找到一个简洁的答案来说明如何建立这个系统。

---EDIT------

- - -编辑- - - - - -

So I set up a quick example using the information below.

因此,我使用下面的信息建立了一个快速的示例。

the angular: var app = angular.module("api");

角:var app = angular.module(“api”);

app.controller("MainController", ["$scope","$http",MainController]);

function MainController($scope,$http){

    $http.post('/api',{test: "hello"})
        .then(function(response){
            console.log(response.data);
        })

}

The flask: from flask import Flask, request import json

flask:从flask导入flask,请求导入json

app = Flask(__name__)


@app.route('/api', methods=['POST', 'GET'])
def api():
    if request.method == 'POST':
       request.data
    return 'You made it' # Just so I originally could see that the flask page                    

if __name__ == "__main__":
    app.run()

I'm getting a 404 for that URL. If I change the angular to look at 'localhost:5000/api' (where my flask app is running),it gives me the error of "Unsupported URL Type".

这个URL有404。如果我改变角度查看'localhost:5000/api'(我的flask应用程序在那里运行),它会给我“不支持URL类型”的错误。

I am seeing when I do the first case, it tries to look at http://localhost/api , which is correct! except for the port. Which is why I tried to specify the port.

我在处理第一个案例时看到,它试图查看http://localhost/api,这是正确的!除了港。这就是为什么我要指定端口。

Any suggestions for a next step?

下一步有什么建议吗?

1 个解决方案

#1


3  

Use flask.

使用烧瓶。

You could host your app on a flask "server" and return the content you'd like too with a python processing.

您可以在flask“服务器”上托管您的应用程序,并返回您希望使用python处理的内容。

http://flask.pocoo.org/

http://flask.pocoo.org/

Use the documentation to setup a route where you'll POST your data using jquery or whatever, then on the route you can do your python stuff and return a JSON to your angular app if you need to.

使用文档设置一条路径,在该路径中,您将使用jquery或其他工具发布数据,然后在该路径上,您可以执行python操作,并在需要时将JSON返回给您的角应用程序。

 from flask import request   

 @app.route('/test', methods=['POST', 'GET'])
    def test():
        if request.method == 'POST':
            print request.data['your_field']

        return your_json_data

#1


3  

Use flask.

使用烧瓶。

You could host your app on a flask "server" and return the content you'd like too with a python processing.

您可以在flask“服务器”上托管您的应用程序,并返回您希望使用python处理的内容。

http://flask.pocoo.org/

http://flask.pocoo.org/

Use the documentation to setup a route where you'll POST your data using jquery or whatever, then on the route you can do your python stuff and return a JSON to your angular app if you need to.

使用文档设置一条路径,在该路径中,您将使用jquery或其他工具发布数据,然后在该路径上,您可以执行python操作,并在需要时将JSON返回给您的角应用程序。

 from flask import request   

 @app.route('/test', methods=['POST', 'GET'])
    def test():
        if request.method == 'POST':
            print request.data['your_field']

        return your_json_data