Created
April 2, 2014 14:27
-
-
Save twmht/9935260 to your computer and use it in GitHub Desktop.
Simple ajax with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"> | |
</script> | |
<script> | |
function ProcessSimpleCgi() | |
{ | |
param1Data = $("#param1").val(); | |
param2Data = $("#param2").val(); | |
params = "param1=" + param1Data + "¶m2=" + param2Data; | |
$.ajax( | |
{ | |
type: "POST", | |
url: "/cgi-bin/test2.py", | |
data: params, | |
dataType: "html", | |
success: function (html) | |
{ | |
var params = $(html).filter(function(){ return $(this).is('p') }); | |
params.each( | |
function() | |
{ | |
var value = "<li>" + $(this).html() + "</li>"; | |
$("#paramsList").append( value ); | |
} | |
); | |
}, | |
error: function(request, ajaxOptions, thrownError) | |
{ | |
$("#debug").html(request.responseText); | |
$("#debug").html("5566"); | |
} | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<div> | |
<span class="rowName">Number One: </span><input id="param1" type="text"> | |
</div> | |
<div> | |
<span class="rowName">Number Two: </span><input id="param2" type="text"> | |
</div> | |
<div><input type="button" value="Submit Query" onclick="ProcessSimpleCgi()"></div> | |
<ul id="paramsList"></ul> | |
<div id = "debug"></div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cgitb; cgitb.enable() ## This line enables CGI error reporting | |
import BaseHTTPServer | |
import CGIHTTPServer | |
server = BaseHTTPServer.HTTPServer | |
handler = CGIHTTPServer.CGIHTTPRequestHandler | |
server_address = ("", 8080) | |
#This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts | |
#handler.cgi_directories = ["/cgi-bin"] | |
httpd = server(server_address, handler) | |
print("Starting simple_httpd on port: " + str(httpd.server_port)) | |
httpd.serve_forever() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cgi; | |
import cgitb | |
cgitb.enable() | |
def print_header(): | |
print ("""Content-type: text/html\n | |
<!DOCTYPE html> | |
<html> | |
<body>""") | |
def print_close(): | |
print ("""</body> | |
</html>""") | |
def display_data(param1, param2): | |
print_header() | |
print ("<p>Param1 = " + param1 + "</p>") | |
print ("<p>Param2 = " + param2 + "</p>") | |
print_close() | |
def display_error(): | |
print_header() | |
print ("<p>An Error occurred parsing the parameters passed to this script.</p>") | |
print ("<p>Try something like:</p>") | |
print ("<p><strong>http://localhost/SimpleCgi.py?param1=1¶m2=2</strong></p>") | |
print_close() | |
def main(): | |
form = cgi.FieldStorage() | |
if (form.has_key("param1") and form.has_key("param2")): | |
display_data(form["param1"].value, form["param2"].value) | |
else: | |
display_error() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to install and test the scripts