Develop a Web server by yourself
One day, a lady walked through a construction site and saw three workers working. She asked the first person, "what are you doing ?" The first person was a little unhappy and shouted, "Can't you see that I am building bricks ?" The lady was not satisfied with the answer, and then asked the second person what he was doing. The second person replied, "I am building a brick wall ." Then he turned to the first person and said, "Hey, the brick you built has exceeded the wall height. You have to take down the last brick ." The lady was not satisfied with the answer. She then asked the third person what he was doing. The third person looked up at the sky and said to her, "I am building the largest church ever in the world ". When he looked at the sky, the other two had begun to quarrel with the extra brick. He slowly turned to the first two people and said, "Boys, don't worry about that brick. This is an internal wall, and it will be flushed with lime. No one will notice this brick. Next, build the lower layer ."
The story implies that when you master the design of the entire system and understand how different components are combined (bricks, walls, churches, you can quickly discover and solve problems (more bricks ).
But what is the relationship between this story and developing a Web server from scratch?
In my opinion, to become a better programmer, you must better understand the software system you use on a daily basis, this includes programming languages, compilers, interpreters, databases and operating systems, Web servers, and network development frameworks. To better and more deeply understand these systems, you must re-develop them from the ground up and repeat them one by one.
Confucius said: No, no.
No, no
Listen to what others say
Unknown
How others do things
If you do not know, do not know if you do.
Do something yourself
Note: I hear and I forget, I see and I remember, I do and I understand. Foreigners generally think that this sentence is from Confucius. However, when looking for the source of this sentence, I found that there was a blog post saying that the Chinese character of this sentence actually came from Xunzi's "Ru Xiao". it is found that.
I hope that when you read this article, you have accepted the method of re-developing different software systems to learn their principles.
Self-developed Web server is divided into three parts, which will introduce how to develop a simple Web server from scratch. Let's get started.
First, what is a Web server?
HTTP Request/Response
In short, it is a network connection server built on a physical server, waiting for the client to send requests permanently. When the server receives the request, it generates a response and returns it to the client. Communication between the client and the server is based on the HTTP protocol. The client can be a browser or any software that supports HTTP.
What is the simple form of Web server implementation? Below is my understanding of this. The sample code is implemented in the Python language. However, even if you do not understand the Python language, you can understand the related concepts from the code and the following explanations:
import socket
HOST, PORT ='',8888
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print'Serving HTTP on port %s ...'% PORT
whileTrue:
client_connection, client_address = listen_socket.accept()
request = client_connection.recv(1024)
print request
http_response ="""\
HTTP/1.1 200 OK
Hello, World!
"""
client_connection.sendall(http_response)
client_connection.close()
Save the above Codewebserver1.py
Or download the file directly from my Github repository and run the file through the command line:
$ python webserver1.py
Serving HTTP on port 8888…
Next, enter the URL http: // localhost: 8888/hello in the address bar of the browser, and press enter to see the magic scene. "Hello, World!" should appear in the browser !" This sentence:
The browser returns "Hello World ""
Is it amazing? Next, let's analyze the implementation principles behind it.
First, let's look at the network address you entered. Its name is URL (Uniform Resource Locator). Its basic structure is as follows:
Basic URL Structure
Through the URL, you tell the Web server address that the browser needs to discover and connect to, and get the page path on the server. However, before the browser sends an HTTP request, it must first establish a TCP connection with the target Web server. Then, the browser sends an HTTP request to the server through a TCP connection and waits for the server to return an HTTP response. When the browser receives a response, it will display the response content on the page. In the preceding example, the browser displays "Hello, World !" This sentence.
So how does one establish a TCP connection before the client sends a request and the server returns a response? To establish a TCP connection, both the server and client use the so-called socket ). Next, we do not directly use the browser, but usetelnet
Manually simulate the browser.
On the same computer provider running the Web server, enable it once through the command line.telnet
Session, set the host to be connectedlocalhost
The connection port of the host is set8888
And then press Enter:
$ telnet localhost 8888Trying 127.0.0.1 …Connected to localhost.
After completing these operations, you have established a TCP connection with the local running Web server to send and receive HTTP information at any time. The following figure shows the standard process for the server to accept the new TCP connection.
Standard Process for the server to accept TCP connections
In the abovetelnet
In the session, we enterGET /hello HTTP/1.1
And press Enter:
$ telnet localhost 8888Trying 127.0.0.1 …Connected to localhost.GET /hello HTTP/1.1HTTP/1.1 200 OKHello, World!
You have successfully simulated the browser manually! You manually send an HTTP request and receive an HTTP response. The following figure shows the basic structure of the HTTP request:
Basic Structure of HTTP requests
The HTTP request line includes the HTTP method.GET
Method, because we want to obtain the content from the server), the server page path (/hello
) And HTTP Version.
To simplify it as much as possible, the current Web server does not parse the above request. You can enter code that is meaningless or receive "Hello, World! "Response.
After you enter the Request Code and press the Enter key, the client sends the request to the server. The server will parse the request and return the corresponding HTTP response.
The following figure shows the HTTP Response Details returned from the server to the client:
HTTP Response Parsing
Let's analyze it. The response contains status lines.HTTP/1.1 200 OK
, Followed by a required blank line, followed by the body of the HTTP response.
Response status lineHTTP/1.1 200 OK
Contains the HTTP Version, HTTP status code, and the Reason Phrase (Reason Phrase) corresponding to the status code ). After the browser receives the response, the body of the response is displayed, Which is why "Hello, World!" is displayed in the browser !" This sentence.
This is the basic working principle of Web servers. A brief review: the Web server first creates a listener socket (listening socket) and enables a perpetual cycle to receive new connections. After the client starts a TCP connection with the server and successfully establishes a connection, after an HTTP request is sent to the server, the server returns an HTTP response. To establish a TCP connection, both the client and server use sockets.
Now that you have a basic and available simple Web server, you can use a browser or other HTTP client for testing. As shown abovetelnet
Command and manually enter the HTTP request, you can also become an HTTP client.
The following is a question: how to run the Djando application, Flask application, and Pyramid application on the server without making any modifications to the server code, what are the requirements of these different network frameworks?
The answer will be shown in the second part of the self-developed Web Server series.
For more details, please continue to read the highlights on the next page: