Use python to develop the simplest web server

Use python to develop the simplest web server

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for processing

The following article comes from Tencent cloud Author: Tang Yin

(Want to learn Python? Python learning exchange group: 1039645993, to meet your needs, the information has been uploaded to the group file stream, you can download it yourself! There are also a large number of the latest 2020 python learning materials.)

Today, I will teach you how to use the rich image resources you have available to build a picture-viewing website. The important thing is not the quantity of knowledge, but the quality of knowledge. Some people know a lot, but don t know the most useful things.

Prepare information: python 3 +pycharm (editor)

Library: flask: an open source python web server framework

Jinja2: Flask's default template engine

One of the simplest web server

Python provides us with an interface: WSGI: Web Server Gateway Interface, which only requires Web developers to implement a function to respond to HTTP requests. Without touching the TCP connection, HTTP original request and response format. The following example is the simplest web application: environ: a dict object containing all HTTP request information; start_response: a function to send HTTP responses. Put the two scripts in the same directory, run server.py, and you can see the effect. Processing URLs In fact, web applications are processing different URLs. We will modify hello.py so that it will process two urls,'/' and'/signin'. The above method using the template is too tiring and slow. Let's learn a little bit more easily, just go to the code: Note, this is a single file . To analyze this script: Flask automatically associates the URL with the function internally through the Python decorator. After starting up and running, we visit'/', the page we see is a "HOME" word,'/signin', at the moment it is accessed via GET, what we see is a form, fill in'admin' and'password', and click login >'/signin', it is accessed through POST at this moment, but it is Hello, admin! or Bad username or password. For students who do not understand GET, POST and HTML forms, it is recommended to learn the basics of html. But this is still a bit inflexible. The content that users access and see needs to be written out and cannot be reused. It is too troublesome to use the template engine template to solve our above problems. First look at a piece of code. Here we read the names of all the files in the root directory and pass them to the html template page. Then, create a directory templates in the same directory as .py. Here are our templates. The special feature of the templates is that they can be used. Python commands and variables are in html home.html

{% %} is written in the command {{ }} is written in variables, so the final result is that multiple tags will be generated, and the name of the tag is the directory name. The finished product uses the pictures we crawled last time to build the site, good idea! Here, create a static directory in the same directory of the .py script to store the pictures. (Pictures in the outer layer of the directory where the .py is located will not be linked) Then the template file welcome.html port is a random port, ranging from 2000 to 2100, I believe everyone can find it. If the external network is still inaccessible, please turn off the firewall! ! This section is just to give you a demonstration, how to write a web server in python, personally feel much more interesting than apache+php! If this article is helpful to your study-you can like it + follow! Will continue to update more new articles. thank!