這幾篇博客均來自python核心編程html
若是你有任何疑問,歡迎聯繫我或者仔細查看這本書的地20章python
另外推薦下這本書,但願對學習python的同窗有所幫助web
概念預熱數據庫
python -m CGIHTTPServer 8000
這會在當前目錄創建一個CGI web服務器編程
這樣客戶端的請求就能夠調用執行python腳本了也就是CGI程序瀏覽器
建立一個test目錄,而後在這個目錄執行上面的代碼,開啓一個cgihttp服務器ruby
而後穿件代碼和文件目錄以下:服務器
每一個文件的代碼看這裏:框架
1 <HTML><HEAD><TITLE> 2 Friends CGI Demo (static screen) 3 </TITLE></HEAD> 4 <BODY><H3>Friends list for: <I>NEW USER</I></H3> 5 <FORM ACTION="/cgi-bin/friends1.py"> 6 <B>Enter your Name:</B> 7 <INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15> 8 <P><B>How many friends do you have?</B> 9 <INPUT TYPE=radio NAME=howmany VALUE="0" CHECKED> 0 10 <INPUT TYPE=radio NAME=howmany VALUE="10"> 10 11 <INPUT TYPE=radio NAME=howmany VALUE="25"> 25 12 <INPUT TYPE=radio NAME=howmany VALUE="50"> 50 13 <INPUT TYPE=radio NAME=howmany VALUE="100"> 100 14 <P><INPUT TYPE=submit></FORM></BODY></HTML>
1 #!/usr/bin/env python 2 3 import cgi 4 5 reshtml = '''Content-Type: text/html\n 6 <HTML><HEAD><TITLE> 7 Friends CGI Demo (dynamic screen) 8 </TITLE></HEAD> 9 <BODY><H3>Friends list for: <I>%s</I></H3> 10 Your name is: <B>%s</B><P> 11 You have <B>%s</B> friends. 12 </BODY></HTML>''' 13 14 form = cgi.FieldStorage() 15 who = form['person'].value 16 howmany = form['howmany'].value 17 print reshtml % (who, who, howmany)
1 #!/usr/bin/env python 2 3 import cgi 4 5 header = 'Content-Type: text/html\n\n' 6 7 formhtml = '''<HTML><HEAD><TITLE> 8 Friends CGI Demo</TITLE></HEAD> 9 <BODY><H3>Friends list for: <I>NEW USER</I></H3> 10 <FORM ACTION="/cgi-bin/friends2.py"> 11 <B>Enter your Name:</B> 12 <INPUT TYPE=hidden NAME=action VALUE=edit> 13 <INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15> 14 <P><B>How many friends do you have?</B> 15 %s 16 <P><INPUT TYPE=submit></FORM></BODY></HTML>''' 17 18 fradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n' 19 20 def showForm(): 21 friends = '' 22 for i in [0, 10, 25, 50, 100]: 23 checked = '' 24 if i == 0: 25 checked = 'CHECKED' 26 friends = friends + fradio % \ 27 (str(i), checked, str(i)) 28 29 print header + formhtml % (friends) 30 31 reshtml = '''<HTML><HEAD><TITLE> 32 Friends CGI Demo</TITLE></HEAD> 33 <BODY><H3>Friends list for: <I>%s</I></H3> 34 Your name is: <B>%s</B><P> 35 You have <B>%s</B> friends. 36 </BODY></HTML>''' 37 38 def doResults(who, howmany): 39 print header + reshtml % (who, who, howmany) 40 41 def process(): 42 form = cgi.FieldStorage() 43 if form.has_key('person'): 44 who = form['person'].value 45 else: 46 who = 'NEW USER' 47 48 if form.has_key('howmany'): 49 howmany = form['howmany'].value 50 else: 51 howmany = 0 52 53 if form.has_key('action'): 54 doResults(who, howmany) 55 else: 56 showForm() 57 58 if __name__ == '__main__': 59 process()
1 #!/usr/bin/env python 2 3 import cgi 4 from urllib import quote_plus 5 from string import capwords 6 7 header = 'Content-Type: text/html\n\n' 8 url = '/cgi-bin/friends3.py' 9 10 errhtml = '''<HTML><HEAD><TITLE> 11 Friends CGI Demo</TITLE></HEAD> 12 <BODY><H3>ERROR</H3> 13 <B>%s</B><P> 14 <FORM><INPUT TYPE=button VALUE=Back 15 ONCLICK="window.history.back()"></FORM> 16 </BODY></HTML>''' 17 18 def showError(error_str): 19 print header + errhtml % (error_str) 20 21 formhtml = '''<HTML><HEAD><TITLE> 22 Friends CGI Demo</TITLE></HEAD> 23 <BODY><H3>Friends list for: <I>%s</I></H3> 24 <FORM ACTION="%s"> 25 <B>Your Name:</B> 26 <INPUT TYPE=hidden NAME=action VALUE=edit> 27 <INPUT TYPE=text NAME=person VALUE="%s" SIZE=15> 28 <P><B>How many friends do you have?</B> 29 %s 30 <P><INPUT TYPE=submit></FORM></BODY></HTML>''' 31 32 fradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n' 33 34 def showForm(who, howmany): 35 friends = '' 36 for i in [0, 10, 25, 50, 100]: 37 checked = '' 38 if str(i) == howmany: 39 checked = 'CHECKED' 40 friends = friends + fradio % \ 41 (str(i), checked, str(i)) 42 print header + formhtml % (who, url, who, friends) 43 44 reshtml = '''<HTML><HEAD><TITLE> 45 Friends CGI Demo</TITLE></HEAD> 46 <BODY><H3>Friends list for: <I>%s</I></H3> 47 Your name is: <B>%s</B><P> 48 You have <B>%s</B> friends. 49 <P>Click <A HREF="%s">here</A> to edit your data again. 50 </BODY></HTML>''' 51 52 def doResults(who, howmany): 53 newurl = url + '?action=reedit&person=%s&howmany=%s' % \ 54 (quote_plus(who), howmany) 55 print header + reshtml % (who, who, howmany, newurl) 56 57 def process(): 58 error = '' 59 form = cgi.FieldStorage() 60 61 if form.has_key('person'): 62 who = capwords(form['person'].value) 63 else: 64 who = 'NEW USER' 65 66 if form.has_key('howmany'): 67 howmany = form['howmany'].value 68 else: 69 if form.has_key('action') and \ 70 form['action'].value == 'edit': 71 error = 'Please select number of friends.' 72 else: 73 howmany = 0 74 75 if not error: 76 if form.has_key('action') and \ 77 form['action'].value != 'reedit': 78 doResults(who, howmany) 79 else: 80 showForm(who, howmany) 81 else: 82 showError(error) 83 84 if __name__ == '__main__': 85 process()
首先來訪問咱們的htm文件:localhost:8000/friends.htm編程語言
你會獲得一個web表單頁面,查看它的源代碼,你將看到當你提交的時候將會調用friends1.py文件,輸入一些值提交後悔轉到另一個頁面,這個頁面就是friends1.py生成的html頁面,可是你在本地是找不到相關html文件的,在friends1.py裏,print出了須要返回的頁面信息
實際上能夠整合這兩個頁面在一塊兒,你在瀏覽器訪問:localhost:8000/cgi-bin/friends2.py,這個時候瀏覽器並不會返回這個文件裏面的內容,而是執行這個文件,這個文件默認返回了即print了那個friends.htm的源碼,因此你獲得的和你訪問localhost:8000/friends.htm獲得的同樣,你輸入一些字符進行提交,將會發現跳轉到一個頁面,這個頁面也是這個py文件產生的,可是卻在本地找不到任何的htm文件
friends3.py包含了一個返回頁面,這裏不介紹