Section NO. 256, web framework

Source: Internet
Author: User

Web Framework

Web Framework Nature

As we all know, for all Web applications, is essentially a socket server, the user's browser is actually a socket client.

Example:

#!/usr/bin/env python#Coding:utf-8ImportSocket#importing a single-threaded communication moduledefHandle_request (client):#Send content functionBUF = CLIENT.RECV (1024)#set Maximum transfer bytesClient.sendall (Bytes ("http/1.1 ok\r\n\r\n", encoding='Utf-8'))#Send content to the client, sent in bytesClient.sendall (Bytes ("Hello, World Welcome to visit", encoding='Utf-8'))#send content to the clientdefMain ():#Create a communication functionSock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)#creating a service-side communication objectSock.bind (('localhost', 8082))#setting server-side IP and ports on the serverSock.listen (5)#listen for IP and port, set a parameter that indicates the maximum number of connections queued     whileTrue:#LoopsConnection, address = sock.accept ()#waiting for the client to receive the request, once a client requests a connection, it will return two values, one is the connection object, one is the client's address information, so two variables are required to receiveHandle_request (Connection)#Execute the Handle_request function to pass the client request connection to the Handle_request functionConnection.close ()#Close Connectionif __name__=='__main__':#WDS system under if __name__ = = "__main__" To create a process, we debug okay, later in the Linux system does not have this problemMain ()#executes the main function

The above is achieved through the socket, but for the real development of the Python Web program, it is generally divided into two parts: server programs and Applications. The server program is responsible for encapsulating the socket server and collating the various data requested when the request arrives. The application is responsible for the specific logical processing. In order to facilitate the development of the application, there are many web frameworks, such as Django, Flask, web.py and so on. Different frameworks have different ways of developing them, but in any case, the applications you develop will have to work with the server program to provide services to the user. In this way, the server program needs to provide different support for different frameworks. This chaotic situation is bad for both the server and the framework. For the server, you need to support a variety of frameworks, for the framework, only the servers that support it can be used by the development of the application. Standardization becomes particularly important at this time. We can set up a standard that is supported by the framework as long as the server program supports this standard, so they can work with it. Once the criteria are determined, both parties are implemented. In this way, the server can support more frameworks that support standards, and the framework can use more servers that support standards.

Therefore, for the web framework is divided into two categories , one is to include the socket server, there is a logical processing, a class is only the logical processing needs to do a different socket Server

The standalone WSGI server provided by the Python standard library is called Wsgiref.

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.

Wsgiref module, the Python standard library provides a standalone WSGI server called Wsgiref, which is the web framework logic processing

Section NO. 256, web framework

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.