cgi的詳細資料能夠參考 http://httpd.apache.org/docs/2.4/howto/cgi.htmlhtml
下面是一個python實現的cgi腳本,裏面體現了一些cgi的用法,使用其餘腳本實現也都相似。Python可能有一些更簡單的方式,好比cgi庫。python
這個例子可以處理get和post兩種請求,並獲取兩種請求的參數。數據庫
#!/usr/bin/python #coding=utf-8 import psycopg2 import os import sys #返回信息中必須有Content-type print "Content-type: text/html\n\n" #經過環境變量REQUEST_METHOD得到請求方法 if os.environ["REQUEST_METHOD"] == "GET": #cgi get請求經過系統變量 QUERY_STRING 來獲取參數 limit = os.environ["QUERY_STRING"].split("=")[1] else: #cgi post請求從標準輸入中得到參數 limit = sys.stdin.readline().split("=")[1] #從數據庫中讀取數據 conn = psycopg2.connect(database="svmanager", user="svmanager", password="bT9Mkkhu7XBkz7uBa1UNHx", host="192.168.1.56", port="5432") print "==========================" cur = conn.cursor() cur.execute("SELECT vm_name , vm_type from vms limit %s offset 0;" % limit ) rows = cur.fetchall() print "<table style='border:1px solid black'>" for row in rows: print "<tr><td>", row[0], "</td><td>" , row[1] ,"</td></tr>" print "</table>" print "========================" conn.close()