[Python CGI Programming] (1) Hello World

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

2011052701061733.png


转载于:https://www.cnblogs.com/cadenwu/articles/2059365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值