引言
我們知道web應(yīng)用的本質(zhì)就是:
- 瀏覽器發(fā)送一個HTTP請求
- 服務(wù)器收到請求,處理業(yè)務(wù)邏輯,生成html、json等數(shù)據(jù)
- 服務(wù)器把html、json等數(shù)據(jù)放在HTTP響應(yīng)的body中發(fā)送給瀏覽器
- 瀏覽器收到http響應(yīng)
可以看到這一過程我們需要接受、解析HTTP請求和發(fā)送HTTP響應(yīng),如果這些都由我們自己來寫的話,我們需要自己處理包括建立TCP連接(HTTP協(xié)議是建立在TCP之上)、解析原始HTTP請求等工作,這太麻煩了。所以我們需要:
- 一個HTTP服務(wù)器軟件幫我們處理這些工作
- Web應(yīng)用框架專注于處理業(yè)務(wù)邏輯
而WSGI就是約定HTTP服務(wù)器軟件和Web應(yīng)用框架交互的協(xié)議
WSGI協(xié)議
WSGI協(xié)議主要包括兩部分,服務(wù)端和應(yīng)用框架端
具體來說,服務(wù)端就是HTTP服務(wù)器把HTTP原始請求(字節(jié)形式)封裝成一個dict對象,調(diào)用應(yīng)用框架的如下函數(shù)application,dict對象傳給environ參數(shù),并提供一個start_response回調(diào)函數(shù)。
應(yīng)用框架處理完業(yè)務(wù)邏輯之后,回過頭來調(diào)用start_response這個函數(shù)讓HTTP服務(wù)器軟件發(fā)送HTTP響應(yīng)給瀏覽器
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b'<h1>Hello, web!</h1>']

Gunicon
gunicorn是一個用python寫的實(shí)現(xiàn)了WSGI協(xié)議的HTTP Server,也就是HTTP服務(wù)器
我們來看一下它是如何啟動我們的項(xiàng)目的
# 創(chuàng)建虛擬環(huán)境
virtualenv --python=python3 venv
# 安裝gunicorn
pip install gunicorn
# 查看我們的應(yīng)用代碼
cat myapp.py
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b'<h1>Hello, web!</h1>']
# 這行命令的意思是gunicorn從myapp這個模塊中導(dǎo)入application這個對象
# 相當(dāng)于 from myapp import application
# 然后開啟四個worker來處理瀏覽器發(fā)送過來的http請求
# 要注意的是,進(jìn)程不共享內(nèi)存,所以每個worker都實(shí)例化了一個application對象,這在有些場景下或許是一個問題
gunicorn -w 4 myapp:application
[2020-12-22 07:03:22 -0800] [50121] [INFO] Starting gunicorn 20.0.4
[2020-12-22 07:03:22 -0800] [50121] [INFO] Listening at: http://127.0.0.1:8000 (50121)
[2020-12-22 07:03:22 -0800] [50121] [INFO] Using worker: sync
[2020-12-22 07:03:22 -0800] [50124] [INFO] Booting worker with pid: 50124
[2020-12-22 07:03:22 -0800] [50125] [INFO] Booting worker with pid: 50125
[2020-12-22 07:03:22 -0800] [50126] [INFO] Booting worker with pid: 50126
[2020-12-22 07:03:22 -0800] [50127] [INFO] Booting worker with pid: 50127
也就是任何python web框架只要實(shí)現(xiàn)了這個application函數(shù)或者有實(shí)現(xiàn)了__call__方法的對象,就可以了就可以被gunicorn調(diào)用,一定程度上起到了解耦的作用
class Application(object):
def __call__(environ,start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b'<h1>Hello, web!</h1>']
我們自己來實(shí)現(xiàn)HTTP 服務(wù)器軟件或者叫WSGI Server呢?
代碼有點(diǎn)長,建議在電腦上慢慢看,邏輯很簡單
- 創(chuàng)建socket對象
- 開啟一個循環(huán),從socket對象中不停接受客戶端的連接
- 連接建立了就開始接收數(shù)據(jù)(字節(jié)),把數(shù)據(jù)封裝成
environ對象(dict) - 調(diào)用應(yīng)用框架的
application函數(shù),傳入envirion和start_response參數(shù)
vim my_wsgi_server.py
# -*- coding: UTF-8 -*-
import io
import socket
import sys
class WSGIServer(object):
address_family = socket.AF_INET
socket_type = socket.SOCK_STREAM
request_queue_size = 1
def __init__(self, server_address):
# Create a listening socket
self.listen_socket = listen_socket = socket.socket(
self.address_family,
self.socket_type
)
# Allow to reuse the same address
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind
listen_socket.bind(server_address)
# Activate
listen_socket.listen(self.request_queue_size)
# Get server host name and port
host, port = self.listen_socket.getsockname()[:2]
self.server_name = socket.getfqdn(host)
self.server_port = port
# Return headers set by Web framework/Web application
self.headers_set = []
def set_app(self, application):
self.application = application
def serve_forever(self):
listen_socket = self.listen_socket
while True:
# 輪詢獲取客戶端的TCP連接
self.client_connection, client_address = listen_socket.accept()
# 處理一個HTTP請求,然后關(guān)閉
self.handle_one_request()
def handle_one_request(self):
request_data = self.client_connection.recv(1024)
self.request_data = request_data = request_data.decode('utf-8')
# Print formatted request data a la 'curl -v'
print(''.join(
f'< {line}\n' for line in request_data.splitlines()
))
self.parse_request(request_data)
# 把原始的HTTP請求變成dict字典
env = self.get_environ()
# 這里就是WSGI協(xié)議部分
# 傳入包含請求信息的dict對象和回調(diào)函數(shù)start_response
result = self.application(env, self.start_response)
# Construct a response and send it back to the client
self.finish_response(result)
def parse_request(self, text):
request_line = text.splitlines()[0]
request_line = request_line.rstrip('\r\n')
# Break down the request line into components
(self.request_method, # GET
self.path, # /hello
self.request_version # HTTP/1.1
) = request_line.split()
def get_environ(self):
env = {}
# The following code snippet does not follow PEP8 conventions
# but it's formatted the way it is for demonstration purposes
# to emphasize the required variables and their values
#
# Required WSGI variables
env['wsgi.version'] = (1, 0)
env['wsgi.url_scheme'] = 'http'
env['wsgi.input'] = io.StringIO(self.request_data)
env['wsgi.errors'] = sys.stderr
env['wsgi.multithread'] = False
env['wsgi.multiprocess'] = False
env['wsgi.run_once'] = False
# Required CGI variables
env['REQUEST_METHOD'] = self.request_method # GET
env['PATH_INFO'] = self.path # /hello
env['SERVER_NAME'] = self.server_name # localhost
env['SERVER_PORT'] = str(self.server_port) # 8888
return env
def start_response(self, status, response_headers, exc_info=None):
# Add necessary server headers
server_headers = [
('Date', 'Mon, 15 Jul 2019 5:54:48 GMT'),
('Server', 'WSGIServer 0.2'),
]
self.headers_set = [status, response_headers + server_headers]
# To adhere to WSGI specification the start_response must return
# a 'write' callable. We simplicity's sake we'll ignore that detail
# for now.
# return self.finish_response
def finish_response(self, result):
try:
status, response_headers = self.headers_set
response = f'HTTP/1.1 {status}\r\n'
for header in response_headers:
response += '{0}: {1}\r\n'.format(*header)
response += '\r\n'
for data in result:
response += data.decode('utf-8')
# Print formatted response data a la 'curl -v'
print(''.join(
f'> {line}\n' for line in response.splitlines()
))
response_bytes = response.encode()
self.client_connection.sendall(response_bytes)
finally:
self.client_connection.close()
SERVER_ADDRESS = (HOST, PORT) = '', 8888
def make_server(server_address, application):
server = WSGIServer(server_address)
server.set_app(application)
return server
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit('Provide a WSGI application object as module:callable')
# 獲取python my_wsgi_server.py后面的第一個參數(shù)
app_path = sys.argv[1]
module, application = app_path.split(':')
# myapp
module = __import__(module)
# myapp.application
application = getattr(module, application)
# 創(chuàng)建http服務(wù)器
httpd = make_server(SERVER_ADDRESS, application)
print(f'WSGIServer: Serving HTTP on port {PORT} ...\n')
httpd.serve_forever()
我們用自己寫的wsgi server調(diào)用自己寫的application,也就是應(yīng)用框架
python3 my_wsgi_server.py myapp:application
至此,你就成功用自己寫的wsgi server運(yùn)行了自己的應(yīng)用代碼
你還可以嘗試用這個wsgi server運(yùn)行flask
pip3 install flask
vim flask_app
from flask import Flask
flask_app = Flask(__name__)
@flask_app.route('/')
def hello_world():
return "hello flask"
# python3 my_wsgi_server.py flask_app:flask_app
python3 my_wsgi_server.py flask_app:flask_app
訪問8888端口
git clone git@github.com:johnson329/flask_src.git
git checkout 6723f55
virtualenv --python=python3 venv
source venv/bin/activate
pip3 install -r requirements.txt
python3 my_wsgi_server.py flask_app:flask_app
參考
Let’s Build A Web Server. Part 1.