WSGI (Web server Gateway Interface) is a specification that defines the interface format between Web apps and Web servers written in Python, enabling decoupling between Web apps and Web servers.
The standalone WSGI server provided by the Python standard library is called Wsgiref.
1 fromWsgiref.simple_serverImportMake_server2 3 4 defrunserver (environ, start_response):5Start_response ('OK', [('Content-type','text/html')])6 return[Bytes ('', encoding='Utf-8'), ]7 8 9 if __name__=='__main__':Tenhttpd = Make_server ("', 8000, Runserver) One Print("serving HTTP on port 8000 ...") AHttpd.serve_forever ()
View Code
Explanation of the origin of Environ parameters
Simply get the URL from the user input and process the request
1 #!usr/bin/env python2 #-*-coding:utf-8-*-3 4 #Wsgiref followed the WSGI specification, the internal help us to achieve the socket,5 fromWsgiref.simple_serverImportMake_server6 7 defnew ():8F=open ("reg.html","R")9Data=F.read ()Ten f.close () One returnData A defindex (): - return "Index" -urls={ the "/new": New, - "/index": Index, - } - defrunserver (environ, start_response): +Start_response ('OK', [('Content-type','text/html')]) -url=environ['Path_info'] + ifurl=="/index": Aret=index () at elif: url=="/new" -ret=New () - Else: -ret="404" - returnret - if __name__=='__main__': inhttpd = Make_server ("', 8000, Runserver) -Httpd.serve_forever ()
View Code
This kind of writing business logic is very bad, once the URL is many, it is very troublesome, improve as follows:
1 #!usr/bin/env python2 #-*-coding:utf-8-*-3 4 #Wsgiref followed the WSGI specification, the internal help us to achieve the socket,5 fromWsgiref.simple_serverImportMake_server6 7 defnew ():8F=open ("reg.html","R")9Data=F.read ()Ten f.close () One returnData A defindex (): - return "Index" -urls={ the "/new": New, - "/index": Index, - } - defrunserver (environ, start_response): +Start_response ('OK', [('Content-type','text/html')]) -url=environ['Path_info'] + ifUrlinchUrls.keys (): AFunc_name=Urls[url] atret=Func_name () - Else: -ret="404" - returnret - if __name__=='__main__': -httpd = Make_server ("', 8000, Runserver) inHttpd.serve_forever ()
View Code
Files are processed in order to step into the individual files of the management web framework
In fact, both the MVC and the MTV idea are to classify each file and perform each task.
Mvc
Models: Data-related operations
Views: template HTML file
Controllers business logic
Mtv
Models: Data-related operations
Temp;ates: Template HTML file
Views business logic
The nature of the Python web framework