Skip to content

Instantly share code, notes, and snippets.

@pdiazq
Forked from twmht/index.html
Created April 5, 2019 19:56
Show Gist options
  • Save pdiazq/03ee52dfb8a36349fb3c03aa785dfddb to your computer and use it in GitHub Desktop.
Save pdiazq/03ee52dfb8a36349fb3c03aa785dfddb to your computer and use it in GitHub Desktop.
Simple ajax with python
<!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 + "&param2=" + 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>
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()
#!/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&param2=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