Python Learning Note-day17-web Framework

Source: Internet
Author: User

The network communication between the Web server and the Web client is essentially the socket communication, and the server side is running the Socketserver client running the socketclient.


For Python web programs, generally divided into two parts, the server program \ Application, (JSP seems to be the same, reminds me of the Java War package, not too understand, only know that these war packages can form the JSP application).

Server program: is responsible for the socket server encapsulation, when the request arrives, the request is organized,

Application: Responsible for the specific business logic processing,


In order to facilitate the development of the application, there are many web frameworks, Python's web framework has Django Flask web.py tornado, etc.

Different frameworks are developed differently, but both require application and server programs to work with and provide services to users.

But the server to provide support for different frameworks, without a unified standard, then the situation will be very chaotic, both for the server and the framework is not good, the key is that development will be very tired. So standards are important,

Developed in accordance with a unified standard, if both server programs and applications support this unified standard, then both can be used together, once the standard is determined, each side is implemented (like screws and nuts)


This allows the server to support more frameworks that support the standard, and the framework can use more servers that support standards (mutual benefit)



WSGI (Web server Gateway Interface) is a low-level interface between a Web server and a Web application or application framework that can enhance the common denominator of portable web application development.


Wsgi is like a bridge, connected to the Web server, the other side connected to the user's application. However, the bridge has a weak function and sometimes requires other bridges to help with it.



SERVER <---> Wsgi <---> Apps


We can start by writing a simple framework:


struct.py

#!/usr/bin/env python#coding:utf-8from wsgiref.simple_server import make_server def  index ():    return  ' index '  def login ():     return   ' Login '      urlpatterns = [     ('/index/', index),      ('/login/', login),] def runserver (environ, start_response):     start_response (' 200 ok ',  [(' Content-type ',  ' text/html ')])      url = environ[' Path_info ']    func = none    for  item in urlpatterns:        if item[0] == &NBSP;URL:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FUNC&NBSP;=&NBSP;ITEM[1]             break    if  func:   &Nbsp;    return func ()     else:         return  ' 404 not found '      if __name__ ==   ' __main__ ':     httpd = make_server (',  8000, runserver)      print  "serving http on port 8000 ..."     httpd.serve_forever ()



Template engine:

Reference JINJA2 http://timesnotes.blog.51cto.com/1079212/1748411




When using the web framework, we need to be aware of the two names MVC MTV (not music TV)


MVC: Refers to model-view-Controller mode

# Models views Controllers

# database processing HTML template processing request (function)


MTV: Refers to the model-template-View mode

# Models templates views

# database processing HTML template processing request (function)


This article is from the "'ll Notes" blog, so be sure to keep this source http://timesnotes.blog.51cto.com/1079212/1748582

Python Learning Note-day17-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.