shoold
shoold

Reputation: 31

Calling a python function over the web using AJAX?

I want to send a string to a python function I have written and want to display the return value of that function on a web page. After some initial research, WSGI sounds like the way to go. Preferably, I don't want to use any fancy frameworks. I'm pretty sure some one has done this before. Need some reassurance. Thanks!

Upvotes: 3

Views: 1296

Answers (3)

BC.
BC.

Reputation: 24918

In addition to Flask, bottle is also simple and WSGI compliant:

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

Upvotes: 3

miku
miku

Reputation: 188054

You can try Flask, it's a framework, but tiny and 100% WSGI 1.0 compliant.

from flask import Flask
app = Flask(__name__)

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

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

Note: Flask sits on top of Werkzeug and may need other libraries like sqlalchemy for DB work or jinja2 for templating.

Upvotes: 5

sberry
sberry

Reputation: 132018

You can use 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

This is just a very simple example. Also, your content-type may be json or plain text... just wanted to show an example.

Upvotes: 3

Related Questions