標籤:img 自訂 mtv 圖片 def == 尾碼 技術 拆分
Model (資料庫操作) View (模板檔案) Controller(業務處理)
簡稱MVC
也可以稱MTV
Model Template (類似View) Controller
一個簡單web架構
# -*- coding:utf-8 -*-# Author:Brownyangyangfrom wsgiref.simple_server import make_serverdef RunServer(environ,start_response): #environ 封裝用戶端發來的資料 #start_response 封裝要返回給使用者的資料 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) return [‘<h1>Hello,web!</h1>‘.encode(‘utf-8‘),]if __name__ == ‘__main__‘: # 把ip,連接埠,函數傳給make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever()
訪問不同尾碼
# -*- coding:utf-8 -*-# Author:Brownyangyangfrom wsgiref.simple_server import make_serverdef handle_index(): return [‘<h1>This is index!</h1>‘.encode (‘utf-8‘), ]def handle_data(): return [‘<h1>This is data!</h1>‘.encode (‘utf-8‘), ]def RunServer(environ,start_response): #environ 封裝用戶端發來的資料 #start_response 封裝要返回給使用者的資料 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) current_url = environ[‘PATH_INFO‘] if current_url == ‘/index‘: return handle_index() elif current_url == ‘/data‘: return handle_data() else: return [‘<h1>404</h1>‘.encode (‘utf-8‘), ]if __name__ == ‘__main__‘: # 把ip,連接埠,函數傳給make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever()View Code
再最佳化下
# -*- coding:utf-8 -*-# Author:Brownyangyangfrom wsgiref.simple_server import make_serverdef handle_index(): return [‘<h1>This is index!</h1>‘.encode (‘utf-8‘), ]def handle_data(): return [‘<h1>This is data!</h1>‘.encode (‘utf-8‘), ]URL_DICT = { "/index":handle_index, "/data":handle_data,}def RunServer(environ,start_response): #environ 封裝用戶端發來的資料 #start_response 封裝要返回給使用者的資料 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) current_url = environ[‘PATH_INFO‘] func = None if current_url in URL_DICT: func = URL_DICT[current_url] if func: return func() else: return [‘<h1>404!</h1>‘.encode (‘utf-8‘), ]if __name__ == ‘__main__‘: # 把ip,連接埠,函數傳給make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever()如何把上面拆分成MVC呢:執行指令碼a.py如下:# -*- coding:utf-8 -*-# Author:Brownyangyangfrom wsgiref.simple_server import make_serverfrom Controller import accountURL_DICT = { "/index":account.handle_index, "/data":account.handle_data,}def RunServer(environ,start_response): #environ 封裝用戶端發來的資料 #start_response 封裝要返回給使用者的資料 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) current_url = environ[‘PATH_INFO‘] func = None if current_url in URL_DICT: func = URL_DICT[current_url] if func: return func() else: return [‘<h1>404!</h1>‘.encode (‘utf-8‘), ]if __name__ == ‘__main__‘: # 把ip,連接埠,函數傳給make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever()
在a.py當前路徑建立View、Controller和Model
View下面建一個index.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><h1>index @uu</h1></body></html>
Controller下面建一個account.py,專門存放自訂方法
# -*- coding:utf-8 -*- # Author:Brownyangyangimport timedef handle_index(): current_time = str(time.time()) f = open(‘View/index.html‘,mode=‘rb‘) data = f.read() f.close() data = data.replace(b‘@uu‘,current_time.encode(‘utf-8‘)) return [data, ]def handle_data(): return [‘<h1>This is data!</h1>‘.encode (‘utf-8‘), ]
Model沒寫,還沒有這麼複雜
ps:瞭解下wsgiref模組吧
WSGI(Web Server Common Interface)是專門為Python語言制定的web伺服器與應用程式之間的網關介面規範,通俗的來說,只要一個伺服器擁有一個實現了WSGI標準規範的模組(例如apache的mod_wsgi模組),那麼任意的實現了WSGI規範的應用程式都能與它進行互動。因此,WSGI也主要分為兩個程式部分:伺服器部分和應用程式部分。
wsgiref則是官方給出的一個實現了WSGI標準用於示範用的簡單Python內建庫,它實現了一個簡單的WSGI Server和WSGI Application(在simple_server模組中),主要分為五個模組:simple_server, util, headers, handlers, validate。
python web架構