Multi-thread Web Server
This project aims to develop a socket program to implement a Web service using the HTTP protocol.
Downald from GIT: https://github.com/FoeverA0/Multi-thread-Web-Server.git
Function
- create a connection socket when contacted by a client (browser);
- receive the HTTP request from this connection;
- parse the request to determine the specific file being requested;
- get the requested file from the server’s file system;
- create an HTTP response message consisting of the requested file preceded by header lines;
- send the response over the TCP connection to the requesting client. If the client requests a file that is not present in the server, the server will return a “404 Not Found” error message.
Note
Your can run server program, and then test server program by sending requests from the client programs running on different hosts. You may run the server on your own computer, using the IP address of 127.0.0.1. If you run server on a host that already has a Web server running on it, then you should use a different port than port 80 for your Web server.
Technical routes
I develop my code in two stages. In the first stage, I simply implement the server program to receive the HTTP request messages and display the contents. After this is running properly, I add the code to generate appropriate responses in the second stage. The Web server have a log file to record statistics of the client requests. Each request corresponds to one line of record in the log. Write down client hostname/IP address, access time, requested file name and response type for each record. My Web server also can handle some simple errors, such as web-page not found.
Note
I use Python languages for the project. When implementing the Web server, I use basic socket programming classes to build the Web server from scratch instead of using the HTTPServer class directly.
main menu
code
client_connection.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)
client_connection.ioctl(socket.SIO_KEEPALIVE_VALS,(1, 60*1000, 30*1000))
code of main menu
<html>
<head>
<link rel="icon" href="data:,">
<title>Index</title>
<link rel="icon" href="data:,">
</head>
<body>
<h3>Welcome to the Multi-thread Web Server.</h3>
<p>Here's a link to <a href="helloworld.html">alice.txt</a>.</p>
<p>Here's a link to <a href="image.html">image.jpg</a>.</p>
<p>Here's a link to <a href="404.html">404</a>.</p>
<p>Here's a link to <a href="textlog.txt">textlog.txt</a>.</p>
</body>
</html>
return 200 or 304
else:
if dt1 < ifmodified and first is not True:
response = 'HTTP/1.1 304 Not Modified\nLast_Modified: {0}\n'.format(modify_time)
client_connection.sendall(response.encode())
# return 304 because the file not modified
else:
ifmodified = datetime.now()
response = 'HTTP/1.1 200 OK\nLast_Modified: {0}\n'.format(modify_time)
#command finish successfully
first1 = True
client_connection.sendall(response.encode())
fin = open('htdocs' + filename)
content = fin.read()
fin.close()
response = 'If-Modified-Since: '+now_time+'\n\n'+content
client_connection.sendall(response.encode())
# This implements the GET command txt file function of this program
After running the program, enter the http://127.0.0.1:8000 in the browser, at this time the browser will send a GET command like a web server running in the background, and the run function in the program starts a subprocess to call the handle_request function after receiving the command, where the user’s request is further processed. For example, this time we send the GET command to determine the last modified date and if-modified-since date of the file after the if statement determines that we want to open a file that is not an image file and decides the return value, because we are the first time to access this server , and the file we access exists on the server, we get a return value of 200 ok, which allows us to display the returned file normally.
Multi-thread Web Server