?代碼編寫
web接口:開發(fā)一個接口函數(shù)
def application(..):
..
web服務器:python內(nèi)置的服務器
實現(xiàn)了web服務網(wǎng)關接口的服務器[web server gateway interface:wsgi]
wsgiref.simple_server.make_server()
?代碼解釋
web接口函數(shù):網(wǎng)關接口函數(shù),所有瀏覽器發(fā)送的請求,都要進入該函數(shù)
def application(env, response):
# env:請求環(huán)境參數(shù)
# response:響應數(shù)據(jù)參數(shù)
response(‘200 OK’, [‘Content-type’, ‘text/html;charset=utf-8’])
# 返回數(shù)據(jù):因為要通過網(wǎng)絡傳輸,要將數(shù)據(jù)編碼成字節(jié)數(shù)據(jù)進行傳輸
msg = ‘<h1>哈哈哈哈</h1>’.encode(‘utf-8’)
return [msg]
web服務器:使用python內(nèi)置的實現(xiàn)了wsgi協(xié)議的web服務器
from wsgiref.simple_server import make_server
將接口函數(shù)部署到服務器中
http = make_server(‘’, 8000, application)
啟動服務器
http.serve_forever()