Model (Database operation) View (template file) Controller (Business processing)
Abbreviation MVC
It can also be called MTV
Model Template (like view) Controller
A simple web framework
#-*-coding:utf-8-*-#Author:brownyangyang fromWsgiref.simple_serverImportMake_serverdefRunserver (environ,start_response):#environ encapsulation of data sent from clients #Start_response encapsulates the data to be returned to the userStart_response ('OK',[('Content-type','text/html')]) return[''. Encode ('Utf-8'),]if __name__=='__main__': #Pass IP, port, function to Make_serverhttpd = Make_server ("', 8000, Runserver)Print("serving HTTP on port 8000 ...") Httpd.serve_forever ()
accessing different suffixes
#-*-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 encapsulate the data sent by the client #start_response encapsulate the data to be returned to the user Start_response (' K OK ', [(' Content-type ', ' text/html ')]) Current_url = environ[' path_info '] if Current_url = = '/inde X ': Return Handle_index () elif Current_url = = '/data ': Return Handle_data () Else:return [' <H1>404</H1>'. Encode (' Utf-8 '),]if __name__ = = ' __main__ ': # pass IP, port, function to Make_server httpd = Make_server (", 8000,runserver) Print ("Serving HTTP on port 8000 ...") Httpd.serve_forever ()View Code
Re-optimization under
#-*-coding:utf-8-*-#Author:brownyangyang fromWsgiref.simple_serverImportMake_serverdefHandle_index ():return[''. Encode ('Utf-8'), ]defhandle_data ():return[''. Encode ('Utf-8'),]url_dict= { "/index": Handle_index,"/data": Handle_data,}defRunserver (environ,start_response):#environ encapsulation of data sent from clients #Start_response encapsulates the data to be returned to the userStart_response ('OK',[('Content-type','text/html')]) Current_url= environ['Path_info'] Func=NoneifCurrent_urlinchUrl_dict:func=Url_dict[current_url]iffunc:returnfunc ()Else: return[''. Encode ('Utf-8'), ]if __name__=='__main__': #Pass IP, port, function to Make_serverhttpd = Make_server ("', 8000, Runserver)Print("serving HTTP on port 8000 ..."Httpd.serve_forever () How to split the above into MVC: Execute the script a.py as follows:#-*-coding:utf-8-*-#Author:brownyangyang fromWsgiref.simple_serverImportMake_server fromControllerImportaccounturl_dict= { "/index": Account.handle_index,"/data": Account.handle_data,}defRunserver (environ,start_response):#environ encapsulation of data sent from clients #Start_response encapsulates the data to be returned to the userStart_response ('OK',[('Content-type','text/html')]) Current_url= environ['Path_info'] Func=NoneifCurrent_urlinchUrl_dict:func=Url_dict[current_url]iffunc:returnfunc ()Else: return[''. Encode ('Utf-8'), ]if __name__=='__main__': #Pass IP, port, function to Make_serverhttpd = Make_server ("', 8000, Runserver)Print("serving HTTP on port 8000 ...") Httpd.serve_forever ()
New view, controller, and model in a.py current path
A index.html is built under view
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title></Head><Body><H1>Index @uu</H1></Body></HTML>
The controller builds a account.py, dedicated to the custom method
#-*-coding:utf-8-*-#Author:brownyangyangImport TimedefHandle_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,]defhandle_data ():return[''. Encode ('Utf-8'), ]
The model isn't written, it's not so complicated.
PS: Understand the next Wsgiref module bar
WSGI (Web server Common Interface) is a specification of the gateway interface between a Web server and an application specifically designed for the Python language, as long as a server has a module that implements the WSGI standard specification (for example, Apache MoD _WSGI module), any application that implements the WSGI specification can interact with it. Therefore, WSGI is also mainly divided into two program parts: the server section and the Application section.
Wsgiref is a simple python built-in library that implements the WSGI standard for demonstration purposes, which implements a simple WSGI server and WSGI application (in the Simple_server module). Mainly divided into five modules: simple_server, util, headers, handlers, validate.
Python Web framework