1. 搭建编程环境
操作系统:
cadenwu@cadenwu:~$ cat /etc/debian_version
6.0.1
Python版本:
cadenwu@cadenwu:~$ python
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
安装Apache:
cadenwu@cadenwu:~$ mkdir /home/cadenwu/cadenwu.net
cadenwu@cadenwu:~$ cd /home/cadenwu/cadenwu.net/;mkdir cgi-bin htdoc lib template
cadenwu@cadenwu:~$ sudo apt-get install apache2
cadenwu@cadenwu:~$ sudo vi /etc/apache2/sites-available/default
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/cadenwu/cadenwu.net/htdoc
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/cadenwu/cadenwu.net/htdoc/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /home/cadenwu/cadenwu.net/cgi-bin/
<Directory "/home/cadenwu/cadenwu.net/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
2. 编写CGI
cadenwu@cadenwu:~$ vi cadenwu.net/htdoc/index.html
cadenwu@cadenwu:~$ more cadenwu.net/htdoc/index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta HTTP-EQUIV="REFRESH" content="0; url=/cgi-bin/homepage.py">
<title>cadenwu.net</title>
</head>
<body>
</body>
</html>
cadenwu@cadenwu:~$ vi cadenwu.net/cgi-bin/homepage.py
cadenwu@cadenwu:~$ chmod +x cadenwu.net/cgi-bin/homepage.py
cadenwu@cadenwu:~$ cat cadenwu.net/cgi-bin/homepage.py
#!/usr/bin/env python
# -*- coding=utf-8 -*-
import cgi
import cgitb
class CoreCGI:
def __init__(self):
self.data = {}
self.form = cgi.FieldStorage()
self.showContent()
cgitb.enable()
def showContent(self):
print "Content-type: text/html\n"
if __name__ == "__main__":
_cgi = CoreCGI()
print "Hello World"
3. "Hello World"
cadenwu@cadenwu:~$ sudo /etc/init.d/apache2 restart