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)
source share