Is it possible to use Python, AJAX and CGI together

I want to have a webpage where you click a button using AJAX. I get a line from a python script and then display that line in an HTML paragraph element.

I know that I can do this using Python, WSGI and AJAX (theoretically I can do this), but its waaaay is too complicated. I have experience with CGI and python.

Can I do this using CGI?

If I can, how does a python script work, exactly the same as when serving a page using CGI?

This does not work:

import cgitb; cgitb.enable() import cgi import os print "Content-Type: text/html\n" input_data = cgi.FieldStorage() print "hello" 

When I click on a button on my page, nothing happens and my CGI server (which works fine for cgi page requests) gives me an http 501 error.

My html and javascript:

  <!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) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 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> 
+4
source share
4 answers

Here are 3 files [my.html, myCGI.py, myPyServer.py]. In Windows XP, I put them all in one directory and double click on myPyServer.py, and everything works very well.

my.html is the same as your html except:

 yours: <a href="javascript:onTest('aaa.py', '')">Click it</a> mine: <a href="javascript:onTest('/myCGI.py', 'x=7')">Click it</a> 

myCGI.py is pretty close to yours

 import cgitb; cgitb.enable() import cgi import os input_data = cgi.FieldStorage() if input_data: print "Content-Type: text/html\n" print "hello" else: f = open('my.html', 'r'); s = f.read(); f.close() print "Content-Type: text/html\n" print s 

myPyServer.py

 import CGIHTTPServer import BaseHTTPServer import sys class Handler(CGIHTTPServer.CGIHTTPRequestHandler): cgi_directories = ["/"] #make sure this is where you want it. [was "/cgi"] PORT = 8000 httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler) # see effbot http://effbot.org/librarybook/thread.htm def runserver(): print "serving at port", PORT httpd.serve_forever() import thread thread.start_new_thread(runserver, ()) print "opening browser" import webbrowser url = 'http://127.0.0.1:8000/myCGI.py' webbrowser.open_new(url) quit = 'n' while not(quit=='quit'): quit = raw_input('\n ***Type "quit" and hit return to exit myPyServer.*** \n\n') print "myPyServer will now exit." sys.exit(0) 
+5
source

Of course, you can use plain old CGI if you want. Your code works fine for me. (When you click the link, "abcdef" goes to "hi.")

You should have a simple configuration error. I would check file permissions on test scripts (a + rx) that might be missing. I also assume that you have "#! / Usr / bin / env python" (or equivalent) at the top of your cgi script (it is omitted in your example above).

+4
source

Check out sajax:

http://www.modernmethod.com/sajax/

There is a python library and an example download. You can have one CGI script that can process your view and implement any AJAX call.

0
source

Here is one simple example of using CGI Python and ajax. http://www.ssiddique.info/writing-your-first-python-cgi-ajax-script.html

0
source

Source: https://habr.com/ru/post/1341753/


All Articles