<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 3.4.4 Python ### 3.4.4 Python Python是一門通用編程語言,由Van Rossum發明,在1994年達到了1.0版。它的應用領域十分廣泛,服務器編程只是其中一部分。 關于Python語言本身,有很多優秀的讀物可供參考。在此作者推薦OReilly出版的[《Learning Python, 5th Edition》](http://shop.oreilly.com/product/0636920028154.do)。這本書內容全面,也相當厚重,對有經驗的開發者來說也許顯得啰嗦。對后者我推薦[《Python in a Nutshell》](http://shop.oreilly.com/product/0636920012610.do?sortby=publicationDate),仍然由OReilly出版。 Python在服務器端的編程主要依賴**WSGI(Web Server Gateway Interface )**。它是一個Python的規范,定義了服務器和應用程序的交互接口[1](#fn_1)。這里“服務器”是指接受客戶端(如瀏覽器)HTTP請求的程序,而“應用程序”是指(由你編寫的)響應HTTP請求的程序。Python的現代Web編程框架都基于WSGI。 一個WSGI的Hello, world程序如下: ``` from wsgiref.simple_server import make_server def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return ['<p>Hello, WSGI!</p>'] httpd = make_server('', 8080, app) httpd.serve_forever() ``` 其中,函數app就是我們的應用程序(WSGI應用程序不僅可以是函數,還可以是具有**call**方法的對象,如后例)。按照WSGI規范,它有兩個參數: - environ是一個dict類型對象,與CGI的環境變量類似,包含了HTTP請求在內的輸入 - start\_response是一個函數,app必須調用它來返回HTTP應答狀態代碼和頭部(header) app返回一個iterable對象,如一個list,作為HTTP應答的消息主體(message body)。 [wsgiref](https://docs.python.org/2/library/wsgiref.html)是Python的一個WSGI工具包,包括一個供開發、測試用的服務器,由make\_server得到。 假設程序保存在文件hello.py中,在命令行上執行 ``` python hello.py ``` 就啟動了我們的WSGI應用程序。在瀏覽器里訪問`http://localhost:8080/`,即可看到: ``` Hello, WSGI! ``` WSGI有一個強大的功能叫做**中間件(middleware)**。舉例來說: ``` from wsgiref.simple_server import make_server def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return ['<p>Hello, WSGI!</p>'] class Middleware: def __init__(self, app): self.wrapped_app = app def __call__(self, environ, start_response): result = self.wrapped_app(environ, start_response) result.insert(0, '<h1>Middleware</h1>') return result httpd = make_server('', 8080, Middleware(app)) httpd.serve_forever() ``` 其中,Middleware對象就是一個中間件,它接受服務器的調用參數,在內部調用下級應用,并返回結果給上級服務器(也能是另一個中間件!)。因此,中間件可以檢查、修改應用程序的輸入、輸出,進而實現各種功能,如session,訪問權限控制,日志,錯誤處理等等。 了解WSGI有助于深刻領會高級的Python Web編程框架,以及更好地使用middleware。關于Python Web編程和WSGI的更多內容,讀者可參考 <https://docs.python.org/2/howto/webservers.html> 此外,這個Wiki列出了一些使用WSGI的編程框架[https://en.wikipedia.org/wiki/Web\_Server\_Gateway\_Interface#WSGI-compatible\_applications\_and\_frameworks](https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface#WSGI-compatible_applications_and_frameworks)。[Django](https://www.djangoproject.com/)是其中比較流行的一種。 > 1. WSGI的官方文檔:<https://www.python.org/dev/peps/pep-3333/>[?](#reffn_1 "Jump back to footnote [1] in the text.")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看