I delve into WSGI, and it's quite complicated.
What I'm trying to do is pretty simple: when I click the link, I want to get the string "hello" from the python script and display "hello" in the HTML paragraph element.
Now I have created python WSGI scripts that display HTML text, i.e. serve the page using WSGI python, but do not use Python, AJAX and WSGI together for the above purpose.
Now when I click the link on my HTML page, the paragraph element displays “error” rather than “hello”. Where do you think I'm wrong? in python or javascript?
Is my python script right?
from wsgiref.simple_server import make_server
from cgi import parse_qs, escape
def application(environ, start_response):
return [ "hello" ]
if __name__ == '__main__':
from wsgiref.simple_server import make_server
srv = make_server('localhost', 8000, application)
srv.serve_forever()
Maybe its my Javascript & / or HTML, where am I mistaken ?:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!--
function onTest( dest, params )
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById( "bb" ).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST",dest,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send( params );
}
-->
</script>
</head>
<body>
<p id="bb"> abcdef </p>
<a href="javascript:onTest('aaa.py', '')">Click it</a>
</body>
</html>
, :
- wsgi script
- http://localhost:8000/test.html
- html ""
wsgi.py, aaa.py test.html .
: - import os wsgiref.simple_server import make_server
FILE = 'index.html'
PORT = 8000
def test_app(environ, start_response):
if environ['REQUEST_METHOD'] == 'POST':
try:
request_body_size = int(environ['CONTENT_LENGTH'])
request_body = environ['wsgi.input'].read(request_body_size)
except (TypeError, ValueError):
request_body = "0"
try:
response_body = str(int(request_body) ** 2)
except:
response_body = "error"
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return [response_body]
else:
f = environ['PATH_INFO'].split( "?" )[0]
f = f[1:len(f)]
response_body = open(f).read()
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():
"""Start the server."""
httpd = make_server("", PORT, test_app)
httpd.serve_forever()
if __name__ == "__main__":
open_browser()
print "Now serving on Port 8000"
start_server()