Write CGI with Python (Lighttpd Server)

来源:互联网 发布:淘宝新开店铺没有流量 编辑:程序博客网 时间:2024/06/05 10:24
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 cgiimport osimport urlparseimport cgitbimport stringcgitb.enable()# Get the query string from environment, it is set by lighttpdquery = os.getenv('QUERY_STRING')sum = 0# Print the headerprint 'Content-Type: text/html'printprint '<title>Python CGI</title>'print '<h1>Test Python for CGI</h1>'# Sum up all the valuesif 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 resultprint '<p>Sum = %d</p>' %sum
4. OK, it is very easy, have fun!


原创粉丝点击