python web編程CGI,pythonwebcgi
CGI(通用閘道介面),CGI 是Web 服務器運行時外部程式的規範,按CGI 編寫的程式可以擴充伺服器功能。
CGI 應用程式能與瀏覽器進行互動,還可通過資料庫API 與資料庫伺服器等外部資料源進行通訊,從資料庫伺服器中擷取資料。
格式化為HTML文檔後,發送給瀏覽器,也可以將從瀏覽器獲得的資料放到資料庫中。幾乎所有伺服器都支援CGI,可用任何語言編寫CGI。
配置讓apache支援.py檔案,我使用的是phpstudyIntegration Environment,不知道咋回事,windows裡面這個斜杠我怎麼打都沒報錯,如果使用的是linux,請遵守交通規則(斜杠別打錯了)
一個簡單的 Web 表單從 HTML 程式碼中可以看到,該表單包括兩個輸入變數:user 和 age。這兩個欄位的值將會傳到 CGI 指令碼pe_info.py 中:
檔案名稱:pe_info.html
<!DOCTYPE html><html lang="en"><title>hello py</title><body><form method="POST" action="cgi-bin/pe_info.py"> <b>你的名字:</b> <input type="text" name="user" placeholder="username" size="15"> <p> <b>你的年齡:</b> <input type="radio" name="age" value="0-15" checked> 0-15 <input type="radio" name="age" value="16-30"> 16-30 <input type="radio" name="age" value="31-45"> 31-45 <input type="radio" name="age" value="46-60"> 46-60 <input type="radio" name="age" value="61-999"> 61-999 </p> <input type="submit"></form></body></html>
檔案名稱:pe_info.py
#!C:\Users\smelond\AppData\Local\Programs\Python\Python36-32\python.exe# _*_ coding:utf-8 _*_# File_type:返回個人資訊# Filename:pe_info.py# Author:smelondimport cgiimport cgitb # 若想在瀏覽器中看到的是 Web 應用程式的回溯資訊,而不是“內部伺服器錯誤”,可以使用 cgitb 模組cgitb.enable()form = cgi.FieldStorage() # FieldStorage 的執行個體,包含 user 和 howmany 欄位的值print("Content-Type:text/html\n") # 注意,如果沒有這條,可能會提示500錯誤print("<title>hello py</title>")if not 'user' in form: print("no input name")else: user = form["user"].value # 擷取變數中的值 age = form["age"].value print("<h1>%s:%s</h1>" % (user, age))
注意看第一行,我使用的是python3的絕對路徑,而不是#!/usr/bin/env python,否則可能會出現500錯誤
效果:
提交之後:
繞過上面的靜態檔案,將兩個檔案合并到一個檔案內,最終的指令碼可以用動態產生的 HTML 檔案輸出表單和結果頁面,並且知道在何時輸出哪個頁面。
#!C:\Users\anjing\AppData\Local\Programs\Python\Python36-32\python.exe# _*_ coding:utf-8 _*_# File_type:單頁面返回提交的個人資訊# Filename:pe_info1.py# Author:smelondimport cgiimport cgitbcgitb.enable()print("Content-Type:text/html\n")formhtml = """ <html> <head> <title>CGI</title> </head> <body> <form method="POST" action=""> <b>請輸入你的名字:</b> <input type="text" name="user" placeholder="username" size="15"><br> <input type="hidden" name="action" value="edit"> <b>請填寫你的年齡:</b> %s <input type="submit"> </form> </body> </html>""" # 上面有一個type=hidden用來判斷是否提交fradio = '<input type="radio" name="age" value="%s" %s> %s\n'def showForm(): friends = [] for i in ("1-15", "16-30", "31-45", "46-60", "60-999"): checked = '' if i == "16-30": checked = "checked" friends.append(fradio % (str(i), checked, str(i))) # 把這些數字分別寫到fradio裡面,然後添加到friends列表中 print("%s" % formhtml % "".join(friends)) # 將每個選項按鈕用join拆分後放到formhtml裡面,然後再放到%s裡面,然後列印出來reshtml = """ <html> <head><title>rehtml CGI</title></head> <body> 你的名字:<b>%s</b><p><hr> 你的年齡:<b>%s</b><hr> </body> </html>"""def doResults(user, howmany): print(reshtml % (user, howmany))def process(): form = cgi.FieldStorage() if "user" in form: user = form["user"].value else: user = "no user" if "age" in form: age = form["age"].value else: age = 0 if "action" in form: # 當我們點擊了提交,FieldStorage執行個體裡面會有MiniFieldStorage('action', 'edit'),上面說過hidden用來判斷是否提交 doResults(user, age) else: # 如果過沒有action,表示我們還沒有提交,FieldStorage裡面什麼都沒有,所以執行showForm()函數 showForm()if __name__ == '__main__': process()