The way to use CGI in Python and in the XAMPP under Win-7:
1.install Python and XAMPP first and add the env var of Python:
2.install XAMPP:
3.Add the mysql env var for the system:
E:\xampp\mysql\bin
4.chang the password for the root user:
5. add the following lines to the file E:\xampp\apache\conf\httpd.conf :
AddHandler cgi-script .cgi .pl .asp .py
AddType text/html .shtml .py
Options Indexes FollowSymLinks Includes ExecCGI
6.new a file named hello.py:
you should notice the right path of the python.exe or it will be error by Apache
#!C:\Python27\python.exe
print "Content-Type: text/html\n"
print """<html><title>hello Crusher</title><body><h1>Hello Python CGI <a href="http://www.conecrusherplant.net">Crusher</a></h1></body></html>"""
test:
7. try the CGI with the Mysql and the data:
sql:
create database books;
use books;
create table book (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255)
);
insert into book (name) values ("Django book");
insert into book (name) values ("Python Book");
8.the python code:
#!C:\Python27\python.exe
import MySQLdb
print "Content-Type: text/html\n"
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"
connection = MySQLdb.connect(user='root', passwd='zoosuck', db='books')
cursor = connection.cursor()
cursor.execute("SELECT name from book;")
for row in cursor.fetchall():
print "<li>%s</li>" % row[0]
print "</ul>"
print "</body></html>"
connection.close()
9.the data in the page:
Now, you can programming with Python and CGI!