<form method="POST" action="http://host.com/cgi-bin/test.py"> <p>Your first name: <input type="text" name="firstname"> <p>Your last name: <input type="text" name="lastname"> <p>Click here to submit form: <input type="submit" value="Yeah!"> <input type="hidden" name="session" value="1f9a2"> </form>
Python CGI programming
一个典型的cgi程序
#!/usr/local/bin/python import cgi def main(): print "Content-type: text/html " form = cgi.FieldStorage() # parse query if form.has_key("firstname") and form["firstname"].value != "": print "<h1>Hello", form["firstname"].value, "</h1>" else: print "<h1>Error! Please enter first name.</h1>" main()
典型架构
form = cgi.FieldStorage()
if not form:
...display blank form...
elif ...valid form...:
...perform action, display results (or next form)...
else:
...display error message (maybe repeating form)...