Web Server/WebServer.py
def handle_request(tcp_socket, client_ip, client_port):
    """Receive and response to HTTP request message.

    Args:
        :param tcp_socket: the socket which is created from start_server function.
        :param client_ip: IP address of the client.
        :param client_port: Port number of the client.

    Raises:
        FileNotFoundError: file does not exist.
    """
    print("Client ({ip}: {port}) is coming...".format(ip = client_ip, port = client_port))
    try:
        # 1. Receive request message from the client on connection socket.
        msg = tcp_socket.recv(1024).decode()
        if not msg:
            print("Error! server receive empty HTTP request.")
            tcp_socket.close()
        # Create new object (handlestr) from strProcess class.
        handle_str = StrProcess(msg)
        # 2. Extract the path of the requested object from the message (second part of the HTTP header).
        file_name = handle_str.split_str()[1:]
        # 3. Find the corresponding file from disk.
        # Check whether the requested object exists or not.
        f = open(file_name)
        f.close()
        # If object exists, ready to send "HTTP/1.1 200 OK\r\n\r\n" to the socket.
        status = "200 OK"
    except FileNotFoundError:
        # Else, ready to send "HTTP/1.1 404 Not Found\r\n\r\n" to the socket.
        status = "404 Not Found"
        file_name = "NotFound.html"
    re_header = "HTTP/1.1 " + status + "\r\n\r\n"
    # 4. Send the correct HTTP response.
    tcp_socket.send(re_header.encode())
    # 5. Store in temporary buffer.
    with open(file_name, 'rb') as f:
        file_content = f.readlines()
    # 6. Send the content of the file to the socket.
    for strline in file_content:
        tcp_socket.send(strline)
    # 7. Close the connection socket.
    print("Bye to Client ({ip}: {port})".format(ip = client_ip, port = client_port))
    tcp_socket.close()
Receive and response to HTTP request message.

    Args:
        :param tcp_socket: the socket which is created from start_server function.
        :param client_ip: IP address of the client.
        :param client_port: Port number of the client.

    Raises:
        FileNotFoundError: file does not exist.
    
Receive and response to HTTP request message.
Receive and response to HTTP request message .
def handle_request(tcp_socket, client_ip, client_port):
    print("Client ({ip}: {port}) is coming...".format(ip = client_ip, port = client_port))
    try:
        msg = tcp_socket.recv(1024).decode()
        if not msg:
            print("Error! server receive empty HTTP request.")
            tcp_socket.close()
        handle_str = StrProcess(msg)
        file_name = handle_str.split_str()[1:]
        f = open(file_name)
        f.close()
        status = "200 OK"
    except FileNotFoundError:
        status = "404 Not Found"
        file_name = "NotFound.html"
    re_header = "HTTP/1.1 " + status + "\r\n\r\n"
    tcp_socket.send(re_header.encode())
    with open(file_name, 'rb') as f:
        file_content = f.readlines()
    for strline in file_content:
        tcp_socket.send(strline)
    print("Bye to Client ({ip}: {port})".format(ip = client_ip, port = client_port))
    tcp_socket.close()
def handle_request ( tcp_socket , client_ip , client_port ) : print ( "Client ({ip}: {port}) is coming..." . format ( ip = client_ip , port = client_port ) ) try : msg = tcp_socket . recv ( 1024 ) . decode ( ) if not msg : print ( "Error! server receive empty HTTP request." ) tcp_socket . close ( ) handle_str = StrProcess ( msg ) file_name = handle_str . split_str ( ) [ 1 : ] f = open ( file_name ) f . close ( ) status = "200 OK" except FileNotFoundError : status = "404 Not Found" file_name = "NotFound.html" re_header = "HTTP/1.1 " + status + "\r\n\r\n" tcp_socket . send ( re_header . encode ( ) ) with open ( file_name , 'rb' ) as f : file_content = f . readlines ( ) for strline in file_content : tcp_socket . send ( strline ) print ( "Bye to Client ({ip}: {port})" . format ( ip = client_ip , port = client_port ) ) tcp_socket . close ( )
Receive and response to HTTP request message.
Receive and response to HTTP request message .
"""Receive and response to HTTP request message.

    Args:
        :param tcp_socket: the socket which is created from start_server function.
        :param client_ip: IP address of the client.
        :param client_port: Port number of the client.

    Raises:
        FileNotFoundError: file does not exist.
    """ # 1. Receive request message from the client on connection socket. # Create new object (handlestr) from strProcess class. # 2. Extract the path of the requested object from the message (second part of the HTTP header). # 3. Find the corresponding file from disk. # Check whether the requested object exists or not. # If object exists, ready to send "HTTP/1.1 200 OK\r\n\r\n" to the socket. # Else, ready to send "HTTP/1.1 404 Not Found\r\n\r\n" to the socket. # 4. Send the correct HTTP response. # 5. Store in temporary buffer. # 6. Send the content of the file to the socket. # 7. Close the connection socket.
file does not exist.
file does not exist .
FileNotFoundError
tcp_socket
client_ip
client_port
param client_port: Port number of the client.
param client_port : Port number of the client .