Guide |
I believe that if you want to be a better developer, you have to have a deeper understanding of the internal structure of the software systems you use daily, including programming languages, compilers and interpreters, databases and operating systems, Web servers, and web frameworks. And to get a better understanding of these systems, you have to start from scratch and use brick by brick to reconstruct the system. |
One day, a woman walking was passing by a construction site and saw three working workers. She asked the first person, "What are you doing?" The first man shouted, "Don't you see I'm building bricks?" The woman was dissatisfied with the answer, and asked the second man, "What are you doing?" The second man replied, "I'm building a brick wall." He turned to the first man and said to him, "Hey, you've got the wall over your head." Go get that brick down! However, the woman was still dissatisfied with the answer, and asked the third person the same question. The third man looked up at the sky and said to her, "I am building the largest church in the world." When he answered, the first person and the second man were arguing over the bricks that had just been built. He turned to the two men and said, "Don't worry about that brick." The wall is indoors, it will be filled with cement, and no one will see it. Go and build the next floor. ”
The story tells us that if you can understand the structure of the whole system and understand how the various parts of the system are combined (like bricks, walls and the whole church), you will be able to locate and fix the problem faster (the wrong brick).
If you want to create a WEB server from scratch, what do you need to do?
I believe that if you want to be a better developer, you have to have a deeper understanding of the internal structure of the software systems you use daily, including programming languages, compilers and interpreters, databases and operating systems, Web servers, and web frameworks. And to get a better understanding of these systems, you have to start from scratch and use brick by brick to reconstruct the system.
Xunzi used these words to express this idea:
"No smell, no smell." (I hear and I forget.) "
"If you don't smell it, see it." (I see and I remember.) "
"If you do not know it." (I do and I understand.) "
I hope you can now realize that it is a good idea to rebuild a software system to understand how it works.
In this three-article series, I'll teach you to build your own Web server. Let's get started.
First, the first question: What is a WEB server?
In short, it is a network server running on a physical server (alas, a server-nested server) waiting for the client to send a request to it. When it receives the request, it generates a response and echoes back to the client. The client and the server communicate with each other through the HTTP protocol. The client can be your browser or any other software that uses the HTTP protocol.
What should be the simplest WEB server implementation? Here I give my realization. This example is written by Python, even if you haven't heard of Python (it's a super-easy language to try!) ), you should also be able to understand the concepts in code and annotations:
Import sockethost, PORT = ', 8888listen_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 ... '% portwhile Tr UE: client_connection, client_address = listen_socket.accept () request = CLIENT_CONNECTION.RECV (1024x768) Print request Http_response = ""/http/1.1 Okhello, world! " " " Client_connection.sendall (http_response) client_connection.close ()
Save the above code as webserver1.py, or download the file directly from GitHub. Then, run the program on the command line. Like this:
$ python webserver1.pyserving HTTP on port 8888 ...
Now, enter Url:http://localhost:8888/hello in the address bar of your Web browser, tap Enter, and then witness the miracle. You should see "Hello, world!." appear in your browser, just like that:
Seriously, go ahead and give it a try. I'll be waiting for you when you do the experiment.
It's done? Not bad! Now let's talk about how it actually works.
First we start with the Web address you just typed. It's called the URL, which is its basic structure:
The URL is the address of a Web server that the browser uses to find and connect to the Web server and return the above content to you. Before your browser can send an HTTP request, it needs to establish a TCP connection with the WEB server. The HTTP request is then sent in the TCP connection and waits for the server to return an HTTP response. When your browser receives a response, it will display its contents, in the example above, it shows "Hello, world!".
Let's explore the process of establishing a TCP connection between the client and the server before sending an HTTP request. To establish the link, they use the so-called socket (socket). We don't use the browser to send requests right now, and we use telnet at the command line to manually simulate this process.
On the computer where you are running the WEB server, set up a Telnet session on the command line, specify a local domain name, use port 8888, and then press ENTER:
$ telnet localhost 8888Trying 127.0.0.1 ... Connected to localhost.
At this point, you have established a TCP connection with the server running on your local host. In, you can see a server starting from scratch, to the basic process of being able to establish a TCP connection.
In the same Telnet session, enter Get/hello http/1.1, and enter a carriage return:
$ telnet localhost 8888Trying 127.0.0.1 ... Connected to localhost. Get/hello http/1.1http/1.1 Okhello, world!
You just manually simulated your browser (the work)! You sent an HTTP request and received an HTTP reply. The following is the basic structure of an HTTP request:
The first line of the HTTP request consists of three parts: the HTTP method (get, because we want our server to return some content), and the path to the desired page, Hello, and the protocol version.
For the sake of simplicity, the WEB server we just built has completely ignored the above request content. You can also try to enter some useless content instead of "Get/hello http/1.1", but you will still receive a "Hello, world!" Response.
Once you enter the request line and hit enter, the client sends the request to the server, and the server reads the request line and returns the corresponding HTTP response.
The following is the response of the server back to the client (telnet in the example above):
Let's parse it out. This response consists of three parts: a status line http/1.1 OK, followed by a blank line followed by the response body.
The status line of the HTTP response, http/1.1, contains the HTTP version number, the HTTP status code, and the HTTP status phrase "OK". When the browser receives a response, it displays the response body, which is why you see "Hello, world!" in the browser.
The above is the basic working model of WEB server. To summarize: The WEB server creates a socket in the listening state and loops over the new connection. After the client establishes a successful TCP connection, it sends an HTTP request to the server and the server responds with an HTTP response, and the client displays the HTTP response content to the user. In order to establish a TCP connection, sockets are used by both the client and the server.
Now you should understand how the WEB server works, and you can experiment with a browser or other HTTP client. If you have tried and observed, you should also be able to use Telnet to manually write HTTP requests to become a "humanoid" HTTP client.
Now leave a small question: "How do you fit a Django, Flask or Pyramid application on a WEB server that you just built up without making any changes to the program?" ”
I will explain in detail in the second part of this series. Please look forward to it.
By the way, I'm writing a book called "Build a Web server: start from scratch." This book explains how to write a basic Web server from scratch, which contains more details that are not in this article. Subscribe to the mailing list and you will be able to get the latest progress on this book, as well as the release date.
Set up a WEB server (i)