Static Web server returns the specified page
import socket if __name__ == '__main__': Tcp_server_socket = socket.socket(socket.af_inet, socket.sock_stream) Tcp_server_socket (128) while True: # 4, receive client connection request conn_socket, Ip_port = tcp_server_socket.accept() # 5, receive data (HTTP request packets) recv_client_data = conn_socket.recv(4096) if len(recv_client_data) > 0: Recv_client_content = recv_client_data.decode(' UTF-8 ') # print(recv_client_content) # print(recv_client_content HTTP/1.1) request_path = recv_client_content.split(", maxsplit=2)[1] # print(request_path) # If request_PATH == '/', you should give it access to the project's home page! If request_path == '/': request_path = '/index.html' with open('static' + request_path, 'rb') as f: # 'static' + request_path = static/hlw.png file_data = f.read() except Exception as e: # if the file does not exist, Response_line = 'HTTP/1.1 404 Not Found\r\n' # ② response_header = 'Server:PWB1.0\r\n' # ③ With open('static/error.html', 'rb') as f: Response_data = (response_data + response_data + response_data + response_data + response_data '\r\n').encode('utf-8') + response_body conn_socket.send(response_data) else: Response_header = 'Server:PWB1.0\r\n' # ③ response_body = 'HTTP/1.1 200 OK\r\n' # ② response_header = 'Server:PWB1.0\r\n' # ③ response_body = 'HTTP/1.1 200 OK\r\n' # ② Response_header = 'Server:PWB1.0\r\n Response_data = (response_line + response_header + '\r\n').encode(' utF-8 ') Conn_socket.send (response_data) finally: # 7, close the socket object conn_socket.close()Copy the code
Multitasking version of static Web server
import socket import threading def handle_client_request(conn_socket): Recv_client_data = conn_socket.recv(4096) if len(recv_client_data) > 0: Decode (' utF-8 ') # print(recv_client_content) # print(recv_client_content) # print(recv_client_content request_path = recv_client_content.split(' ', maxsplit=2)[1] if request_path == '/': Try: with open('static' + request_path, 'rb') as f: file_data = f.read() except Exception as e: Response_header = 'Server:PWB1.0\r\n' # ③ response_header = 'Server:PWB1.0\r\n' # ③ Response body with open('static/error.html', 'rb') as f: Response_data = (response_data + response_data + response_data + response_data '\r\n').encode('utf-8') + response_body conn_socket.send(response_data) else: Response_header = 'Server:PWB1.0\r\n' # ③ response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n Encode (' utF-8 '); encode(' utF-8 '); encode(' utF-8 ') response_body conn_socket.send(response_data) finally: conn_socket.close() if __name__ == '__main__': Tcp_server_socket = socket.socket(socket.af_inet, Tcp_server_socket.setsockopt (socket.sol_socket, socket.so_reuseaddr, Tcp_server_socket. bind((", 8080)) # 3, set tcp_server_socket.listen(128) while True: # 4, receive client connection request conn_socket, Ip_port = tcp_server_socket.accept(); ip_port = tcp_server_socket.accept(); Sub_thread.setdaemon (True) sub_thread.start()Copy the code
Object-oriented version of static Web server
Import socket import threading class HttpWebServer(object): def __init__(self): def __init__(self): def __init__(self): Tcp_server_socket = socket.socket(socket.af_inet, Tcp_server_socket.setsockopt (socket.sol_socket, socket.so_reuseaddr, Tcp_server_socket. Bind (('', Self. tcp_server_socket = tcp_server_socket @staticmethod def handle_client_request(conn_socket): Recv_client_data = conn_socket.recv(4096) if len(recv_client_data) > 0: Decode (' utF-8 ') # print(recv_client_content) # print(recv_client_content) # print(recv_client_content request_path = recv_client_content.split(' ', maxsplit=2)[1] if request_path == '/': Try: with open('static' + request_path, 'rb') as f: file_data = f.read() except Exception as e: Response_header = 'Server:PWB1.0\r\n' # ③ response_header = 'Server:PWB1.0\r\n' # ③ Response body with open('static/error.html', 'rb') as f: Response_data = (response_data + response_data + response_data + response_data '\r\n').encode('utf-8') + response_body conn_socket.send(response_data) else: Response_header = 'Server:PWB1.0\r\n' # ③ response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n' # ③ Response_header = 'Server:PWB1.0\r\n Encode (' utF-8 '); encode(' utF-8 '); encode(' utF-8 ') response_body conn_socket.send(response_data) finally: conn_socket.close() def start(self): while True: # 6, receive client connection request conn_socket, Ip_port = self.tcp_server_socket.accept(); ip_port = self.tcp_server_socket.accept(); Sub_thread.start () if __name__ == '__main__': sub_thread.start() if __name__ == '__main__': Web_server.start ()Copy the code