Web静态服务器-1-显示固定的页面
import socket
def handle_client(client_socket):
"为一个客户端进行服务"
recv_data = client_socket.recv(1024).decode("utf-8")
request_header_lines = recv_data.splitlines()
for line in request_header_lines:
print(line)
response_headers = "HTTP/1.1 200 OK\r\n"
response_headers += "\r\n"
response_body = "hello world"
response = response_headers + response_body
client_socket.send(response.encode("utf-8"))
client_socket.close()
def main():
"作为程序的主控制入口"
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("", 7788))
server_socket.listen(128)
while True:
client_socket, client_addr = server_socket.accept()
handle_client(client_socket)
if __name__ == "__main__":
main()
服务器端
客户端