二、静态web
1.请求报文
2.响应头
响应码
3.静态web服务器
python3 -m http.server
http://127.0.0.1:8000/index1.html
4.socket静态页面
import socket if __name__ == '__main__': #创建tcp服务端套接字 tcp_serv_s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #设置端口号复用 tcp_serv_s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True) #端口绑定 tcp_serv_s.bind(("",9000)) #设置监听 tcp_serv_s.listen(128) while True: #等待接受客户端的连接请求 new_socket,ip_port = tcp_serv_s.accept() #代码执行到此,说明连接建立成功 recv_c_d = new_socket.recv(4096) #对接收的客户端的请求包头进行二进制解码 recv_c_con = recv_c_d.decode("utf-8") print(recv_c_con) with open("./index1.html",'rb') as f: f_d = f.read() #相应行 response_l = "HTTP/1.1 200 OK\r\n" #响应头 response_h = "Server: PWS1.0\r\n" #响应体 response_b = f_d #拼接响应报文 res_d = (response_l+response_h+"\r\n").encode("utf-8") + response_b #发送数据 new_socket.send(res_d) new_socket.close()
5.多进程模拟404和200状态码
import multiprocessing import socket def serv_start(port): server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True) server.bind(("",port)) server.listen(128) while True: client,ip_port = server.accept() print(f"客户端{ip_port[0]} 使用{ip_port[1]} 端口连接成功") p = multiprocessing.Process(target=task,args=(client,)) p.start() server.close() def task(client): request_data = client.recv(1024).decode('utf-8') if len(request_data) == 0: client.close() else: request_path = request_data.split(' ')[1] print("请求地址是: ",request_path) print("request: ",request_path) if request_path == '/': request_path = 'index1.html' try: with open('./'+request_path,'rb') as f: file_con = f.read() except Exception as e: response_line = "HTTP/1.1 404 NOT FOUND\r\n" response_head = "Server: PSWS1.1\r\n" with open('./err.html','rb') as f: error_data = f.read() response_data = (response_line+response_head+'\r\n').encode('utf-8') + error_data client.send(response_data) else: response_line = "HTTP/1.1 200 OK\r\n" response_head = "Server: PSWS1.1\r\n" with open('./'+request_path,'rb') as f: response_body = f.read() response_data = (response_line+response_head+'\r\n').encode() + response_body client.send(response_data) finally: client.close() if __name__ == '__main__': serv_start(7777)
6.面向对象多任务
import multiprocessing import socket class server_start(object): def __init__(self,port): self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) self.server.bind(("", port)) self.server.listen(128) def start(self): while True: client,ip_port = self.server.accept() print(f"客户端{ip_port[0]} 使用{ip_port[1]} 端口连接成功") p = multiprocessing.Process(target=self.task,args=(client,)) p.start() self.server.close() def task(self,client): request_data = client.recv(1024).decode('utf-8') if len(request_data) == 0: client.close() else: request_path = request_data.split(' ')[1] print("请求地址是: ",request_path) print("request: ",request_path) if request_path == '/': request_path = 'index1.html' try: with open('./'+request_path,'rb') as f: file_con = f.read() except Exception as e: response_line = "HTTP/1.1 404 NOT FOUND\r\n" response_head = "Server: PSWS1.1\r\n" with open('./err.html','rb') as f: error_data = f.read() response_data = (response_line+response_head+'\r\n').encode('utf-8') + error_data client.send(response_data) else: response_line = "HTTP/1.1 200 OK\r\n" response_head = "Server: PSWS1.1\r\n" with open('./'+request_path,'rb') as f: response_body = f.read() response_data = (response_line+response_head+'\r\n').encode() + response_body client.send(response_data) finally: client.close() if __name__ == '__main__': server_start(7777).start()
结语
点赞👍