Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Principle of django framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

What is a web service

1.web service consists of two parts, http server and app application.

The 2.httpd server is responsible for parsing the data and encapsulating the call socket request

3.app applications are responsible for business logic, including operations such as additions, deletions, modifications and queries.

Once the 4.http server accepts the client request, it parses the data and invokes the app application. After parsing the data, app returns it to the http service and responds to the client.

Communication between client and web service, how to communicate between web service and app application

1. Client communicates with web service

Client and web service communicate using http protocol

Cpact S model

Request requests include:

Part one: request line

Part two: request header

Part III: blank lines

Part IV: request body

Response includes:

The first part: status line, which is composed of HTTP/1.1 (protocol version) 200 (status code) OK (description of status code)

The second part: the response header, which is composed of some key-value pairs, which is used to explain some additional information to be used by the client.

The third part: blank lines, which are necessary to respond to the blank lines behind the head.

Part IV: response text, the text message returned by the server to the client

How to communicate between 2.web service and app

It is necessary to define an interface rule, or protocol, between web service and app application, which specifies the way of data communication between them, that is, how web service invokes app application.

Python stipulates that the interface rule of web service is wsgi protocol, and the definition of wsgi protocol for server and app interfaces is as follows:

How to call server:

Response = application (environ, start_response)

Application API coding:

Def application (environ, start_response):

Status = '200 OK'

Response_headers = [('Content-Type',' text/plain'),]

Start_response (status, response_headers)

Return [baked hellograms,]

As long as the above call mode is followed, the wsgi protocol is implemented.

Python has a built-in wsgiref module to provide server services. Django uses this module to develop tests, but the production environment uses other modules to handle

The flow chart of a web service to app response is as follows:

Principle of django framework

Define a simple wsgi server framework

The server part of the django framework is provided by the built-in wsgiref module of python. We only need to write the application application part.

From wsgiref.simple_server import make_server

Def app (environ, start_response): # the encoding form of the application part specified in the wsgi protocol, which can be extended on this basis

Status = '200 OK'

Respones_headers = []

Start_response (status, response_headers)

Return [baked hellograms,]

If name = 'main':

Httpd = make_server ('127.0.0.1, 8080, app)

Httpd.serve_forever ()

Wsgi Framework request response process:

Principle of django framework

4. Server implementation of django

The server used by django is based on the simple_server of python's built-in wsgiref module. Every time django starts, the run function executes the server_forver function, and the run function calls the server_forver method. This step executes the loop execution of socket_server, so that you can constantly listen to client requests, and server calls application to respond to user requests.

5. Application implementation of django

The application of django is implemented through an instance of WSGIhandle, which can be dropped and then passed in environ and start_response according to the interface rules of wsgi. So in essence, django uses the wsgiref.simple_server provided by the built-in python to encapsulate application richly. Most of the django coding work is in the application part.

VI. Django call chain

Principle of django framework

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report