python之web開發(待續)

來源:互聯網
上載者:User

標籤:

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開發(待續)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.