標籤:
WSGI介面
無論多麼複雜的Web應用程式,入口都是一個WSGI處理函數。
HTTP請求的所有輸入資訊都可以通過environ獲得,HTTP響應的輸出都可以通過start_response()加上函數傳回值作為Body。
其實一個Web App,就是寫一個WSGI的處理函數,針對每個HTTP請求進行響應。
#hello.pydef application(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) return ‘<h1>Hello, web!<h1>‘
#server.pyfrom wsgiref.simple_server import make_serverfrom hello import applicationhttpd = make_server(‘‘, 8000, application)print ‘Serving Http on port 8000‘httpd.serve_forever()
python cgi模組
A CGI script is invoked by an HTTP server, usually to process user input submitted through an HTML <FORM> or <ISINDEX> element.
往往用 FieldStorage 去處理表單資料,且FieldStorage的執行個體就像字典一樣處理。
但如果表單中提交的資料為空白, 那麼那個資料將不會在FieldStorage中顯示, 但可以通過設定keep_blank_values=True使其顯示
ps:
If the submitted form data contains more than one field with the same name, the object retrieved by form[key] is not a FieldStorage or MiniFieldStorage instance but a list of such instances.
If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute.
#form資料在self._environ[‘wsgi.input‘]中, request的資料在self._environ中form = cgi.FieldStorage(fp=self._environ[‘wsgi.input‘], environ=self._environ, keep_blank_values=True)
normal = form[key].value
if isinstance(form[key], list):fileitem = form[key]if fileitem.filename: # It‘s an uploaded file; count lines linecount = 0 while 1: line = fileitem.file.readline() if not line: break linecount = linecount + 1
python之web開發(待續)