Python Network Programming - Static Web Server Multitasking Edition

Static Web Server Multitasking Edition Introduction:

Static Web Server Multitasking Edition.Using multithreading, the web server can handle the access of multiple users at the same time. When the connection between the client and the server is successfully established , a child thread is created, and the child thread is used to process the client's request to prevent the main thread from blocking. Set the created child thread to be the daemon main thread to prevent the main thread from being unable to exit.

Static Web Server Multitasking Edition Overview

Static Web Server Multitasking.Using multithreading, the web server can handle multiple users' access at the same time. When the connection between the client and the server is successfully established , a child thread is created, and the child thread is used to process the client's request to prevent the main thread from blocking. Set the created child thread to be the daemon main thread to prevent the main thread from being unable to exit.

Implementation steps
1. Import the threading module
import threading

2. Set the port number multiplexing, the program exits the port and releases it immediately
tcp_server_ socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , True )

3. When the client and the server establish a connection process, create a child thread
Set the name of the target and pass in the new_socket parameter as a tuple
sub_thread = threading . Thread ( target = handle_client_request , args = ( new_socket ,))

4. Set up the daemon master process
sub_thread.setDaemon ( True ) _ _ _

5. Start the child thread
sub_thread.start ( ) _ _


Code
import socket
import threading

# handle client requests
def handle_client_request ( new_socket ):
# The code is executed here, indicating that the connection is successfully established
recv_client_data = new_socket.recv ( 4096 ) _ _ _
if len ( recv_client_data ) == 0 :
print ( "close the browser" )
new_socket.close ( ) _ _
return

# decode binary data
recv_client_content = recv_client_data.decode ( " utf - 8" )
print ( recv_client_content )
# Split according to the specified string, the maximum number of splits is specified as 2
request_list = recv_client_content.split ( " " , maxsplit = 2 ) _ _

# Get the request resource path
request_path = request_list [ 1 ] _
print ( request_path )

# Determine whether the request is the root directory, if the condition is true, specify the home page data to return
if request_path == "/" :
request_path = "/index.html"

try :
# Dynamically open the specified file
with open ( "static" + request_path , " rb " ) as file :
# read file data
file_data = file.read ( ) _
except Exception as e :
# The requested resource does not exist, return 404 data
# response line
response_line = "HTTP/1.1 404 Not Found "
# response header
response_header = "Server: PWS1.0 "
with open("static/error.html", "rb") as file:
file_data = file.read()
# 响应体
response_body = file_data

# Concatenate the response message
response_data = ( response_line + response_header + " " ) .encode ( "utf-8" ) + response_body
# send data
new_socket.send ( response_data ) _ _ _
else :
# response line
response_line = "HTTP/1.1 200 OK "
# response header
response_header = "Server: PWS1.0 "

# response body
response_body = file_data

# Concatenate the response message
response_data = ( response_line + response_header + " " ) .encode ( "utf-8" ) + response_body
# send data
new_socket.send ( response_data ) _ _ _
finally :
# Close the socket between the service and the client
new_socket.close ( ) _ _


# Program entry function
def main ( ):
# create tcp server socket
tcp_server_socket = socket.socket ( socket.AF_INET , socket.SOCK_STREAM ) _ _ _ _ _ _
# Set the port number multiplexing, the program exits the port and releases it immediately
tcp_server_ socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , True )
# bind port number
tcp_server_socket.bind ( ( " " , 9000 ) )
# set monitor
tcp_server_socket.listen ( 128 ) _ _ _

while True :
# Waiting to accept the connection request from the client
new_socket , ip_port = tcp_server_socket.accept ( ) _ _
print ( ip_port )
# When the client and the server establish a connection process, create a child thread
sub_thread = threading . Thread ( target = handle_client_request , args = ( new_socket ,))
# Set the daemon main thread
sub_thread.setDaemon ( True ) _ _ _
# Start the child thread to execute the corresponding task
sub_thread.start ( ) _ _


if __name__ == '__main__':
main()

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00