实现步骤
1. 获取用户请求资源的路径
2. 根据请求资源的路径,读取指定文件的数据
3. 组装指定文件数据的响应报文,发送给浏览器
4. 判断请求的文件在服务端不存在,组装404状态的响应报文,发送给浏览器
示例
import socket # 获取用户请求资源的路径 # 根据请求资源的路径,读取指定文件的数据 # 组装指定文件数据的响应报文,发送给浏览器 # 判断请求的文件在服务端不存在,组装404状态的响应报文,发送给浏览器 if __name__ == '__main__': # 1.编写一个TCP服务端程序 # 创建socekt tcp_server_socekt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 设置端口复用 tcp_server_socekt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # 绑定地址 tcp_server_socekt.bind(("", 8080)) # 设置监听 tcp_server_socekt.listen(128) while True: # 2.获取浏览器发送的HTTP请求报文数据 # 建立链接 client_socekt, client_addr = tcp_server_socekt.accept() # 获取浏览器的请求信息 client_request_data = client_socekt.recv(1024).decode() if len(client_request_data) <= 1: print("客户端已经关闭") print(client_request_data) else: # 获取用户请求资源的路径 requst_data = client_request_data.split(" ") print(requst_data) # 求资源的路径 request_path = requst_data[1] if request_path == "/": request_path = "/index.html" # 3.读取固定页面数据,把页面数据组装成HTTP响应报文数据发送给浏览器 # 根据请求资源的路径,读取指定文件的数据 try: with open("./static" + request_path, "rb") as f: file_data = f.read() except Exception as e: # 返回404错误数据 # 应答行 response_line = "HTTP/1.1 404 Not Found\r\n" # 应答头 response_header = "Server:pwb\r\n" # 应答体 response_body = "404 Not Found sorry" # 应答数据 # 组装指定文件数据的响应报文,发送给浏览器 response_data = (response_line + response_header + "\r\n" + response_body).encode() client_socekt.send(response_data) else: # 应答行 response_line = "HTTP/1.1 200 OK\r\n" # 应答头 response_header = "Server:pwb\r\n" # 应答体 response_body = file_data # 应答数据 # 组装指定文件数据的响应报文,发送给浏览器 response_data = (response_line + response_header + "\r\n").encode() + response_body client_socekt.send(response_data) finally: # 4.HTTP响应报文数据发送完成以后,关闭服务于客户端的套接字 client_socekt.close()
服务端控制台打印:
['GET', '/test.css', 'HTTP/1.1\r\nHost:', 'localhost:8080\r\nConnection:', 'keep-alive\r\nsec-ch-ua:', '"Chromium";v="118",', '"Google', 'Chrome";v="118",', '"Not=A?Brand";v="99"\r\nsec-ch-ua-mobile:', '?0\r\nsec-ch-ua-platform:', '"Windows"\r\nUpgrade-Insecure-Requests:', '1\r\nUser-Agent:', 'Mozilla/5.0', '(Windows', 'NT', '10.0;', 'Win64;', 'x64)', 'AppleWebKit/537.36', '(KHTML,', 'like', 'Gecko)', 'Chrome/118.0.0.0', 'Safari/537.36\r\nAccept:', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nSec-Fetch-Site:', 'none\r\nSec-Fetch-Mode:', 'navigate\r\nSec-Fetch-User:', '?1\r\nSec-Fetch-Dest:', 'document\r\nAccept-Encoding:', 'gzip,', 'deflate,', 'br\r\nAccept-Language:', 'zh-CN,zh;q=0.9\r\nCookie:', 'Idea-30eca14b=5671be82-bc4b-4154-8aa0-7ea8dc722c2d;', 'Pycharm-a2976cb7=daed6ffd-95bc-4940-b9b4-2562e9a6fdca\r\n\r\n']
['GET', '/favicon.ico', 'HTTP/1.1\r\nHost:', 'localhost:8080\r\nConnection:', 'keep-alive\r\nsec-ch-ua:', '"Chromium";v="118",', '"Google', 'Chrome";v="118",', '"Not=A?Brand";v="99"\r\nsec-ch-ua-mobile:', '?0\r\nUser-Agent:', 'Mozilla/5.0', '(Windows', 'NT', '10.0;', 'Win64;', 'x64)', 'AppleWebKit/537.36', '(KHTML,', 'like', 'Gecko)', 'Chrome/118.0.0.0', 'Safari/537.36\r\nsec-ch-ua-platform:', '"Windows"\r\nAccept:', 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8\r\nSec-Fetch-Site:', 'same-origin\r\nSec-Fetch-Mode:', 'no-cors\r\nSec-Fetch-Dest:', 'image\r\nReferer:', 'http://localhost:8080/test.css\r\nAccept-Encoding:', 'gzip,', 'deflate,', 'br\r\nAccept-Language:', 'zh-CN,zh;q=0.9\r\nCookie:', 'Idea-30eca14b=5671be82-bc4b-4154-8aa0-7ea8dc722c2d;', 'Pycharm-a2976cb7=daed6ffd-95bc-4940-b9b4-2562e9a6fdca\r\n\r\n']
客户端已经关闭
小结
1. 获取用户请求资源的路径 requst_data = client_request_data.split(" ") print(requst_data) # 求资源的路径 request_path = requst_data[1] 2. 根据请求资源的路径,读取请求指定文件的数据 with open("./static" + request_path, "rb") as f: file_data = f.read() 3. 组装指定文件数据的响应报文,发送给浏览器 response_data = (response_line + response_header + "\r\n").encode() + response_body client_socekt.send(response_data) 4. 判断请求的文件在服务端不存在,组装404状态的响应报文,发送给浏览器 except Exception as e: # 返回404错误数据 # 应答行 response_line = "HTTP/1.1 404 Not Found\r\n" # 应答头 response_header = "Server:pwb\r\n" # 应答体 response_body = "404 Not Found sorry" # 应答数据 # 组装指定文件数据的响应报文,发送给浏览器 response_data = (response_line + response_header + "\r\n" + response_body).encode() client_socekt.send(response_data)
标签:web,浏览器,请求,image,socekt,服务器,data,response,页面 From: https://www.cnblogs.com/allenxx/p/17647376.html