WSGI-based web framework

Source: Internet
Author: User

1 Dynamic web Framework

1.1 Web framework to be separate from Web server software (e.g. MVC)

1.2 Web framework to have good interaction with Web servers (Python has a WSGI standard for its own web framework)

1.3 Web framework and database have good read and write communication methods

2 about the WSGI standard

WSGI divides a Web service into two parts: servers and applications. The WGSI server is only responsible for two things related to the network: receiving the HTTP request from the browser, sending an HTTP response to the browser, and the specific processing logic for the HTTP request by invoking the WSGI application.

2.1 Simple WSGI Realization Application

 fromWsgiref.simple_serverImportMake_serverdefWsgi_app (environ, start_response): status='OK'Response_header= [('Content_Type','text/html')] Start_response (status, Response_headers)return 'there is a wsgi app!'httpd= Make_server ('0.0.0.0', 80, Wsgi_app) httpd.serve_forever ()

environ is a dictionary/dict that contains all the HTTP request information and is generated by the WSGI server unpacking the HTTP request.

2.2 Wsgi Model Features:

1. In the Web framework module, take the chestnut above as an example, the Web server software will pass a list (environ) and a reference (start_response) to a function (the function body is implemented in the Web server Software) to the web framework, and then the web framework implements an app function, And will be "a list" and "a function of reference" as two parameters;

2. The passed-in list stores n tuples, which contain the request information of the client browser received by the Web server, a reference to the function arguments passed in, which can be used to return the status feedback of the requested resource (if the requested resource is reachable, it returns 200, if the resource is inaccessible, Returns errors such as 404 or 502;

3. The invocation of the function reference is more forward than the return, which allows the Web server software to prepare for receiving data before returning to the official web page, which can be used as a shortcut to the Web framework and the Web server software to pass the data;

So the Web server framework must implement at least three functions

1. Create a list containing the client request header messages (passed as the first parameter);

2. Create a function that resolves the return status information (passed as the second argument);

3. Receive the body returned by the app function within the web framework and combine the body with the return state value of the function that is the reference to the second parameter, and send it together to the client browser

3 Concrete Implementations

3.1 First need to implement a WISG class as the server

classWSGI (object):def __init__(self, port, app): The socket definition is basically initialized ....#get a reference to the web frameworkSelf.app =appPass        #start the Server object entry function    defRun_forever (self): Self.create_new_socket ()Pass            defCreate_new_socket (self): whileTrue: ...#processing the request accordinglySelf.deal_accept_data (New_client_socket)defdeal_accept_date (Self, new_client_socket):#Receive DataRecv_data = NEW_CLIENT_SOCKET.RECV (1024)        #convert the received data to a listRecv_data_list =Recv_data.splitlines () Gets the request header Request_header=Recv_data_list[0]#the method is to get the request header .../.../...html, for app processing        #get/index.html http/1.1Html_name =get_html (Request_header)#Enter the Dynamic app processing phase        #get the web framework and return data                #Create a dictionary to store the requested parametersEnverion =dict () enverion['Path_info'] =Html_name#starts the web framework for processing and returns the header and body of the responseDynamic_content =Self.app (enverion, Start_response)#Send and Closenew_client_socket.send (dynamic_content) new_client_socket.close ()defMain ():#Import the web framework appApp =__import__(file_name)#start the Web serverWeb_server =WSGI (port, App) Web_server.run_forever ()

3.2 Web framework implemented in accordance with the WSGI standard

defapp (Enverion, start_response):#response can be changed according to the requirements, can be based on the parameters of Environ        #Set the status codeStatus ='OK'    #set the type of page returnedResponse_headers = [('Content_Type','text/html')]          #Incoming header information to function Start_response defined in the web framework    #The headers of response is processed first, and then the content is returned .start_response (status, Response_headers)#returns the body of the required response according to the matching pathPath_name = enverion['Path_info]Content =Process_path (path_name)returncontentdefProcess_path (path_name) Looking for a function to process V_func=Match_path (path_name) content=V_func.return_result ()returnContent

WSGI-based 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.