After an extended research I have arrived to the following example.
The WSGI file:
import threading
import webbrowser
from wsgiref.simple_server import make_server
from cgi import parse_qs
FILE = 'frontend.html'
PORT = 8080
def test_app(environ, start_response):
path = environ.get('PATH_INFO', '').lstrip('/') #get path
if ("static" in path): #handle query for
status = '200 OK' #files in /static
headers = [('Content-type', 'text/javascript')]
start_response(status, headers)
f2serv=file(path,'r') #read file
return environ['wsgi.file_wrapper'](f2serv) #return file
if environ['REQUEST_METHOD'] == 'POST': #If POST...
try:
request_body_size = int(environ['CONTENT_LENGTH'])
request_body = environ['wsgi.input'].read(request_body_size)
except (TypeError, ValueError):
request_body = "0"
parsed_body = parse_qs(request_body)
value = parsed_body.get('test_text', [''])[0] #Returns the first value
try:
response_body = str(int(value) ** 2)
except:
response_body = 'error'
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return [response_body]
else: #If not POST, just pass
response_body = open(FILE).read() #the html file itself
status = '200 OK'
headers = [('Content-type', 'text/html'),
('Content-Length', str(len(response_body)))]
start_response(status, headers)
return [response_body]
def open_browser():
# Start a browser after waiting for half a second
def _open_browser():
webbrowser.open('http://localhost:%s/%s' % (PORT, FILE))
thread = threading.Timer(0.5, _open_browser)
thread.start()
def start_server():
httpd = make_server("", PORT, test_app)
httpd.serve_forever()
if __name__ == "__main__":
open_browser()
start_server()
The html file (named "frontend.html"):
<html>
<head>
<title>AJAX test</title>
</head>
<body>
<script src="static/jquery-1.8.1.js"
type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function submit(){
$.post( "?", { test_text : $("#test_text").val() },
function( data ) {
$('#test_result').html(data);});
};
</script>
square(
<input type="text" name="test_text" id="test_text" value="0" size="4">
) =
<span id="test_result">0</span>
<input type="button" value="compute"
title="compute" onclick="submit();"/>
</body>
</html>
Important: in order to make the example above work, the actual version of jquery.js must be copied under /static (in this case static/jquery-1.8.1.js).