If you want to be a good developer , You should have a deep understanding of the internal structure of the software system that you use every day , Including programming languages 、 Database and operating system 、Web Server and Web frame . and , To better understand these systems , You should start from scratch , Rebuild the system with bricks and tiles .

stay Ruslan The three articles in this series teach us how to create a simple Web The server , To help you better understand web The server , This article is mainly correct Ruslan The translation of three articles and their own understanding , I hope it can help you , In order to better understand this article , You can refer to the agreement forest series Mr , Would you like a list ? (HTTP Overview of the agreement )( Please poke me )

web What is the server

Build your own web The server , The first thing to know is Web What is the server ? Here is a typical web The server , I believe everyone has used :
【 The system design 】 use python Set up a web Server play ( One )
In short ,web The server is a running web server ( It's just a piece of software ), Send a request to its client to wait . When it receives the request , It generates a response , And send it back to the client . Between the client and the server through HTTP Protocol to communicate with each other . The client can be your browser , It can also be used HTTP Any other software of the protocol , Actually , When you read this official account, , What you use is web Services provided by the server .

A simple web Server implementation

commercial web Servers are complex , Hundreds of thousands of lines of code are common , But it can also be simple , It's as simple as a few dozen lines of code . Don't believe it , see Ruslan Sir's first edition web Server code . This example is provided by Python It's written in , Even if you haven't heard of it Python( It's a super easy language to use , Go and have a try !), You should also be able to understand the idea from the code and comments :

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
while True:
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 code as webserver1.py, then , Run the program on the command line . like this :

$ python webserver1.py
Serving HTTP on port 8888 ...

Now? , Enter it in the address bar of your web browser URL:http://localhost:8888/hello , Knock back , And then to witness the miracle . You should see “Hello, World!” Show in your browser , Like the picture below :
【 The system design 】 use python Set up a web Server play ( One )

web Server workflow
Now let's talk about how it actually works . First of all, we start from the Web Address start . It is called URL, This is its basic structure :
【 The system design 】 use python Set up a web Server play ( One )
URL It's a Web The address of the server , The browser uses this address to find and connect Web The server , And return the above to you . Be able to send in your browser HTTP Request before , It needs to work with Web The server creates a TCP Connect . Then in TCP Send in connection HTTP request , And wait for the server to return HTTP Respond to . When your browser receives a response , Its contents will be displayed , In the example above , It shows “Hello, World!”.

Let's explore further in sending HTTP Request before , Establishment of client and server TCP Process of connection . To establish a link , They use it socket( Socket ). We don't send requests directly using browsers right now , On the command line telnet To simulate this process manually .

Before you run Web Server on the computer , Create one on the command line telnet conversation , Specify a local domain name , The use of port 8888, Then press enter :

$ telnet localhost 8888
Trying 127.0.0.1 ...
Connected to localhost.

This is the time , You have set up a server running on your local host TCP Connect . In the following illustration , You can see a server starting from scratch , To be able to establish TCP Basic process of connection .
【 The system design 】 use python Set up a web Server play ( One )
In the same telnet In the session , Input GET /hello HTTP/1.1, Then enter enter enter :

$ telnet localhost 8888
Trying 127.0.0.1 ...Connected to localhost.
GET /hello HTTP/1.1
HTTP/1.1 200 OK
Hello, World!

You've just manually simulated your browser ( The job of )! You sent it HTTP request , And received one HTTP The reply . Here's a HTTP Basic structure of request :
【 The system design 】 use python Set up a web Server play ( One )
HTTP The first line of the request consists of three parts :HTTP Method (GET, Because we want our server to return something ), And indicate the path to the desired page /hello, There's also the protocol version .

For the sake of simplicity , We just built Web The server completely ignored the above request . You can also try to type in some useless content instead of “GET /hello HTTP/1.1”, But you still get one “Hello, World!” Respond to .

Once you enter the request line and type enter , The client sends the request to the server ; The server reads the request line , It will return the corresponding HTTP Respond to .

Here is the server back to the client ( In the example above telnet) Response content of :
【 The system design 】 use python Set up a web Server play ( One )
The response consists of three parts : A status line HTTP/1.1 200 OK, Followed by an empty line , Below is the response text .

HTTP The status line of the response HTTP/1.1 200 OK Contains HTTP Version number ,HTTP Status code 200 as well as HTTP Status phrases “OK”. When the browser receives a response , The text will be displayed in response , That's why you see it in the browser “Hello, World!”.

That's all Web Basic working model of server . To sum up :Web The server creates a socket that is listening , Loop in new connections . Client creation TCP After successful connection , Will be sent to the server HTTP request , Then the server will use a HTTP Respond to respond , The client will HTTP The response content of is displayed to the user . In order to establish TCP Connect , Both the client and the server use sockets .

Now? , You should know Web How the server works , You can use a browser or something HTTP Client to test . If you've tried 、 Observed , You should also be able to use it telnet, Manual compilation HTTP request , Become a “ humanoid ” HTTP client .

Now let's leave a little question :“ How do you want to make no changes to the program , You just built it Web Server adaptation Django, Flask or Pyramid The application? ?” This will be covered in detail in the second part of this series .

Recommended reading :

Carefully organize | Contents of historical dry goods articles
【 welfare 】 Share the excellent course videos collected by myself ( On )
【 System architecture 】 Evolution of large website architecture ( On )
【 System architecture 】 Evolution of large website architecture ( Next )
【C++ Notes 】C++ Memory layout of object model (2)
【C++ Notes 】C++ Memory layout of object model (1)

Focus on server background technology stack knowledge summary and sharing

Welcome to pay attention to communication and common progress

【 The system design 】 use python Set up a web Server play ( One )

There is a way for the farmers coding

There is a way for the farmers , To provide you with easy to understand technical articles , Make technology easier !