First, the nature of web framework
- All Web applications are essentially sockets, and the user's browser is actually a socket client.
- Common web Frameworks in Python are:
- WSGI (Web server Gateway Interface) defines the interface format between Web apps and Web servers that are programmed with Python, enabling decoupling between the server and the client.
- The standalone WSGI server provided by the Pytho Standard library is known as wsgired.
Ii. using wsgrired to customize the web framework
#!/usr/local/bin/python3#-*-coding:utf-8-*-#Author:felix Song; Environment:pycharm 5.0.3 (python3.6)#develop a web framework of your own using the Python standard library fromWsgiref.simple_serverImportMake_server#Second Step transformation, get the content in ConfImportConf_urldefRunserver (environ,start_response): Start_response ('OK',[('Content-type','text/html')]) #The first step, get the user url,debug mode, in Start_response plus breakpoint, then browser access in variables under the Environ findUserurl = environ['Path_info'] Print(userurl) urlpatterns= Conf_url.routes ()#Third Step Transformation #Second Step transformationFunc =None#For item in Conf_url.url: #第三步注释掉 forIteminchUrlpatterns:#Third Step Transformation ifItem[0] = =Userurl:func= Item[1] Break iffunc:returnfunc ()Else: return[Bytes ('', encoding='Utf-8')] " "#第二步, different values are returned depending on the URL input, but if the page is much more laborious with the if else ... Under renovation, create a new conf.py inside define all the URL models if Userurl = = '/index/': return [bytes (' " "if __name__=='__main__': httpd= Make_server ("', 8000, Runserver)Print('serving HTTP on port 8000 ...') Httpd.serve_forever ()
Server
#!/usr/local/bin/python3#-*-coding:utf-8-*-#Author:felix Song; Environment:pycharm 5.0.3 (python3.6)defindex ():return[Bytes ('', encoding='Utf-8')] #return [b ' deflogin ():return[Bytes ('', encoding='Utf-8')]#Third step transformation, routing systemdefroutes (): Urlpatterns= (('/index', index), ('/index', index), ('/index', index)) returnUrlpatterns##第二步改造, Web page comparison#URL = (#('/index ', index),#('/index ', index),#('/index ', index),# )
Conf_url
III. MVC Framework (collation of code)
Model package: Working with databases
View package: Storing HTML files
Login.html:
Controller package: Business logic processing
account.py: Account-related controls are here
admin.py: Background Management
IV. MTV Framework (Classification of code)
Model Package: Operations on the database
Template package: Storing HTML files
View Package: Business logic
Reference:
Http://www.cnblogs.com/wupeiqi/articles/4491246.html
Http://www.cnblogs.com/wupeiqi/articles/5237672.html
Web Framework Introduction