Calling a python function over the Internet using AJAX?

I want to send a string to the python function that I wrote, and I want to display the return value of this function on a web page. After some initial research, WSGI sounds like a path. Preferably, I don't want to use any fancy framework. I'm pretty sure someone did this before. You need to calm down a bit. Thank!

+3
source share
3 answers

You can try Flask , it is a framework, but tiny and 100% compatible with WSGI 1.0.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

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

. Werkzeug , sqlalchemy DB jinja2 .

+5

cgi...

#!/usr/bin/env python

import cgi

def myMethod(some_parameter):
    // do stuff
    return something

form = cgi.FieldStorage()

my_passed_in_param = form.getvalue("var_passed_in")
my_output = myMethod(my_passed_in_param)
print "Content-Type: text/html\n"
print my_output

. , json plain text... .

+3

In addition to Flask bottle is also simple and compatible with WSGI:

from bottle import route, run

@route('/hello/:name')
def hello(name):
    return 'Hello, %s' % name

run(host='localhost', port=8080)
# --> http://localhost:8080/hello/world
+3
source

Source: https://habr.com/ru/post/1755627/


All Articles