Python CGI程式設計需要哪些模組?
Python 的 cgi 模組通常是 Python 中編寫 CGI 程式的起點。cgi 模組的主要目的是從 HTML 表單中提取傳遞給 CGI 程式的值。大多數情況下,使用者都是透過 HTML 表單與 CGI 應用程式進行互動。使用者在表單中填寫一些值來指定要執行的操作的細節,然後呼叫 CGI 使用使用者的規格執行其操作。
你可以在 HTML 表單中包含許多輸入欄位,這些欄位可以是多種不同型別(文字、複選框、選擇列表、單選按鈕等)。
你的 Python 指令碼應該以 `import cgi` 開頭。CGI 模組所做的主要事情是以類似字典的方式處理呼叫 HTML 表單中的所有欄位。你得到的結果並不完全是 Python 字典,但它易於使用。讓我們來看一個例子:
示例
import cgi
form = cgi.FieldStorage() # FieldStorage object to
# hold the form data
# check whether a field called "username" was used...
# it might be used multiple times (so sep w/ commas)
if form.has_key('username'):
username = form["username"]
usernames = ""
if type(username) is type([]):
# Multiple username fields specified
for item in username:
if usernames:
# Next item -- insert comma
usernames = usernames + "," + item.value
else:
# First item -- don't insert comma
usernames = item.value
else:
# Single username field specified
usernames = username.value
# just for the fun of it let's create an HTML list
# of all the fields on the calling form
field_list = '<ul>\n'
for field in form.keys():
field_list = field_list + '<li>%s</li>\n' % field
field_list = field_list + '</ul>\n'我們還需要做更多的事情才能向用戶呈現一個有用的頁面,但是我們已經透過處理提交表單取得了一個良好的開端。
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP