How to learn python from beginner to proficient! Instead of getting started to give up! Guide to send you

Source: Internet
Author: User

You will find that after running the program and using the browser to access the 127.0.0.1:8001, the program will error, the browser says "the page does not work", such as

4. Release the connection TCP connection

If the connection mode is close, the server actively shuts down the TCP connection, the client shuts down the connection passively, releases the TCP connection, and if the connection mode is keepalive, the connection is maintained for a period of time and the request can continue to be received;

5. client browser parsing HTML content

The client browser parses the status line first to see the status code indicating whether the request was successful. Each response header is then parsed, and the response header informs the following character sets for several bytes of HTML documents and documents. The client browser reads the response data HTML, formats it according to the syntax of the HTML, and displays it in a browser window.

HTTP Response message Format:

At this point, you can see the correct page on top of the browser, and you can call up Chrome's developer tools to see the HTTP response format we've sent over.

Return different content based on different paths

Careful you may find that now no matter what path we output, as long as the IP and port numbers remain the same, the browser page shows the same content, which is not quite in line with our daily usage scenarios.

What if I want to return different content based on a different path?

We need to parse the request message received by the server and read the access path.

Observing incoming HTTP requests, it is found that their request lines, request headers, request data are separated by \ r \ n, so we can separate the received requests according to \ r \ n, taking out the access path we want.

At this point, we visit different paths, such as http://127.0.0.1:8001/yimi/http://127.0.0.1:8001/xiaohei/, which will display the content differently on the browser.

As you can see, our current program logic is not very clear, we can change the URL with a list, the URL corresponding to the response is written into a function, through the function call for URL access, you will find that this is the way a framework is handled very much like (laughter 罒 ω 罒 ~ ~ ~)

Return to the specific HTML page

Now, you might think that what we want to return now is returned through a function that returns some simple bytes, what if I want to return a beautifully written HTML page?

We can write the HTML page in the form of binary read in, back to the browser, the browser to parse, this is OK!

Return to Dynamic HTML page

At this point, you may be wondering if you're returning a static, fixed HTML page, and what if I want to return a Dynamic HTML page?

A Dynamic Web page is essentially a substitution of strings, a string substitution occurs on the service side, and is returned to the browser when replaced.

Here, we simulate the return process of a dynamic HTML page by returning a current time.

As you can see, every time we visit the Yimi page, we will return a current time.

A recap

1. The nature of the web framework:

Socket server-to-browser communication

2. Socket server function Partition:

A. Responsible for sending and receiving messages with the browser (socket communication)--Wsgiref/uwsgi/gunicorn

B. Perform different functions depending on the user's access to different paths

C. Read content from HTML, and complete the substitution of strings--JINJA2 (template language)

3. The categorization of Web frameworks in Python:

1. Divided by the above three functions:

1. Frame comes with a,b,c--Tornado

2. Frame comes with B and C, using third-party a-and Django

3. Frame comes with B, using third-party A and C--and Flask

2. Divide by another dimension:

1. Django--and chatty (you can use it for a website)

2. Other--Flask lightweight

You will find that with the Wsgiref module, the program is packaged better and the code logic is clearer.

Next section Trailer

At this point, I believe that you have a clear understanding of the entire browser server's access process, and the socket service-side functionality is clearly defined.

You are welcome to follow my public number: Python Learning communication

Welcome to join thousands of people to exchange questions Base group: 125240963

How to learn python from beginner to proficient! Instead of getting started to give up! Guide to send you

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.