Python,Crusher,XAMPP,CGI

本文提供了一步一步的指南,教你如何在Windows 7系统上安装Python、XAMPP并配置MySQL环境,实现CGI(Common Gateway Interface)的应用。包括设置环境变量、修改配置文件、创建Python CGI脚本以及与数据库进行交互,展示如何通过CGI进行简单的数据操作。

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!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值