Getting Started with Python (18+)
Web minimalist development
Hello everyone!
Well, why is today's opening statement different?
haha, yes! You know this is the catchphrase of our young, handsome and talented Python tutor Liu Zhiang.
I borrowed it here because the easter egg we are going to give you today is exactly what Liu Zhiang’s new video course "Python Web Development" will talk about on Gaodu.com (https://www.igaodu.cn) Oh, let's open a crack here first, let everyone look inside. Because many people start to feel afraid of learning when they see something as tall as "Python Web Development".
But is it really that difficult? Is it really necessary to learn?
Let's put it this way, when we have learned the basics of Python with a heart-warming attitude, we can be regarded as a Python programmer anyway, but where should we go to show our fists? To be honest, learning Python technology can be used in many ways. Web development is one type. Others include data mining, data analysis, machine learning, artificial intelligence. Python can do almost everything you can think of software. Row!
Why start from Web development? This is not necessarily true. However, as a beginner, Web development is indeed a very good entry point to a technical path. After all, this is the age of the Internet. Web applications are extremely common. From websites to platforms, they are all called Web development. The Web is more than a website. Web technology is the foundation and entrance of all network application technologies. It is hard to imagine that today we will create an application independent of the network, which is why we must learn web application technology.
Of course, a complete Web development technology cannot be learned in a single lesson. Interested students can quickly go to the Gaodu website and search for "Python Web Development" to find the complete video course of Teacher Liu Zhiang.
Today we will only do some meaningful explorations on the core concepts of Web development. Through a minimalist case, to get a glimpse of the miracle of Python Web applications, so as to open a door to Web applications for everyone.
Then let's stop talking nonsense, let's take a look at our first WEB application.
1、 The essence of web technology
The simplest Web application is to first save the HTML file, use a ready-made HTTP server software to receive user requests, then read the HTML document information from the file and return it to the browser. Some common static servers do this.
If you want to dynamically generate HTML, you need to implement the above steps yourself. However, accepting HTTP requests, parsing HTTP requests, sending HTTP responses, etc. are not easy things. If we let us write these low-level codes by ourselves, it will take less than 1-2 months to figure it out just by reading the HTTP specification, so this is definitely not the right direction.
The correct approach is that the underlying code is implemented by dedicated server software. We write code in Python, mainly focusing on generating HTML documents. Because we don't want to be exposed to TCP connections, HTTP original request and response formats, we need a unified interface (WSGI interface), let us concentrate on writing Web business logic in Python.
2、 Hello, web
Let's take a look at how python builds a minimalist Web site.
The WSGI interface definition is very simple, it only requires Web developers to implement a function to respond to HTTP requests.
The application() function above is an HTTP processing function that conforms to the WSGI standard, and it receives two parameters:
**environ: a dict object containing all HTTP request information; **
**start_response: A function to send an HTTP response. **
In the application() function, call: start_response() function, the Header tag of the HTTP response is sent. Because the Header can only be sent once, the start_response() function can only be called once.
The start_response() function receives two parameters, one is the http response code, the other is a set of http headers represented by a list, and each header is represented by a tuple containing two strings (str).
Normally, the Content-Type header should be sent to the browser. Many other commonly used HTTP headers should also be sent.
Then, the return value of the function'
This is a server startup program. Python has a built-in WSGI server. To start it, you need to import the module wsgiref, which is a reference implementation of a WSGI server written in pure Python. The so-called "reference implementation" means that the implementation fully complies with the WSGI standard, but does not consider any operational efficiency, and is only for development and testing.
Above: We run server.py in Python,
Started the server,
And connected to the server's 8000 port.
Then, we can open the browser,
Enter: localhost:8000 in the address bar,
You can visit the website.
As shown below.
summary
The real commercial web application is much more complicated than this demo program. If you are interested, please visit the Gaodu website to find the Python web development course of Teacher Liu Zhiang.
So, so far, our Python introductory course is really all shared.
Those who are not yet full of fun, please continue to pay attention to us, and we will launch some very valuable and easy-to-learn courses or content from time to time to share with you.
If you have any questions or corrections, please leave a message to us so that we can improve and provide you with better services.
thank you all!
Recommended Posts