Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Python, AJAX & CGI together

Tags:

python

ajax

cgi

I want to have a web page where you click a button, by using AJAX I get a string from a python script, then display that string in a paragraph HTML element.

I know I can do this by using Python, WSGI & AJAX (theoretically I can do it this way) but its waaaay too difficult. I am experienced with CGI & python.

So can I do the above using CGI?

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

This doesn't work:

import cgitb; cgitb.enable()
import cgi
import os


print "Content-Type: text/html\n"

input_data   = cgi.FieldStorage()
print "hello"

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

My html & 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>
like image 416
Mack Avatar asked Jan 21 '23 08:01

Mack


1 Answers

Here are 3 files [my.html, myCGI.py, myPyServer.py]. In windows XP I put them all in the same directory and double-click on myPyServer.py and things work 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)    
like image 197
Love and peace - Joe Codeswell Avatar answered Jan 31 '23 07:01

Love and peace - Joe Codeswell