Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a Web server

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to build a Web server", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to build a Web server" this article.

Let's start with the first question: what is a Web server?

In short, it is a network server running on a physical server (ah, the server sets the server), waiting for the client to send a request to it. When it receives the request, it generates a response and sends it back to the client. The client and the server communicate with each other through HTTP protocol. The client can be your browser or any other software that uses the HTTP protocol.

What should the simplest Web server implementation look like? Here I give my implementation. This example was written by Python, and even if you haven't heard of Python (it's a super easy-to-use language, try it!), you should be able to understand the ideas in 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, or download the file directly from GitHub. Then, run the program on the command line. Like this:

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

Now, type URL: http://localhost:8888/hello in the address bar of your web browser, hit enter, and witness the miracle. You should see "Hello, World!" Display in your browser, as shown in the following figure:

Seriously, go and have a try. I'll be waiting for you when you do the experiment.

Is it done? Good! Now let's talk about how it actually works.

First of all, let's start with the Web address you just entered. It's called URL, and this is its basic structure:

URL is the address of a Web server that the browser uses to find and connect to the Web server and return the contents to you. Before your browser can send HTTP requests, 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 a HTTP response. When your browser receives a response, it displays its content. In the above example, it shows "Hello, World!".

Let's further explore the process of establishing an TCP connection between the client and the server before sending the HTTP request. To establish links, they use the so-called "socket socket". Instead of using the browser to send the request directly, we use telnet on the command line to simulate the process manually.

On the computer where you are running the Web server, establish a telnet session on the command line, specify a local domain name, use port 8888, and then press enter:

$telnet localhost 8888 Trying 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 the following figure, you can see the basic process of a server starting from scratch to being able to establish a TCP connection.

In the same telnet session, enter GET / hello HTTP/1.1, then 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 just manually simulated your browser! You sent a HTTP request and received a HTTP reply. The following is the basic structure of an HTTP request:

The * line of the HTTP request consists of three parts: the HTTP method (GET, because we want our server to return some content), the path / hello that indicates the desired page, and the protocol version.

For simplicity, the Web server we just built completely ignores the above request. You can also try typing some useless content instead of "GET / hello HTTP/1.1", but you will still receive a "Hello, World!" Respond.

Once you type the request line and hit enter, the client sends the request to the server; the server reads the request line and returns the corresponding HTTP response.

The following is the response that the server returns to the client (telnet in the above example):

Let's analyze it. The response consists of three parts: a status line HTTP/1.1 200OK, followed by a blank line, followed by the response body.

The status line HTTP/1.1 200OK of the HTTP response contains the HTTP version number, the HTTP status code, and the HTTP status phrase "OK". When the browser receives the response, it displays the response body, which is why you see "Hello, World!" in the browser.

The above is all the contents of the article "how to build a Web server". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report