Write CGI with Python (Lighttpd Server)

1. Add the following configurations to your lighttpd.conf:
server.modules	= ( "mod_cgi" )
cgi.assign	= ( 
	".py" => "/usr/bin/python2" 
)
2. Write a simple website as an interface to add two integers:
<html>
  <head>
    <title>Test CGI</title>
  </head>
  <body>
    <form action="/test.py">
      <p>Add two integer, please input two integer:</p>
      <input name="a" size="4"/>
      <input name="b" size="4"/>
      <input type="submit" values="submit"/>
    </form>
  </body>
</html>
3. Write the test.py to add two or more integers:
# Test python for cgi

import os
import urlparse
import cgitb
import string

cgitb.enable()

# Get the query string from environment, it is set by lighttpd
query = os.getenv('QUERY_STRING')
sum = 0

# Print the header
print 'Content-Type: text/html'
print
print '<title>Python CGI</title>'
print '<h1>Test Python for CGI</h1>'

# Sum up all the values
if query is not None:
    pairs = urlparse.parse_qs(query) # pairs is a dict
    for key, value in pairs.items(): # value is a list
        print '<p>key = %s, value = %s</p>' %(key, value)
        sum += string.atoi(value[0])

# Print the result
print '<p>Sum = %d</p>' %sum
4. OK, it is very easy, have fun!


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值