The nature of the Python web framework

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.