1.处理客户端请求数据
>>> s = "GET / HTTP/1.1 Host: 127.0.0.1:8080 Connection: keep-alive" >>> s.splitlines() ['GET / HTTP/1.1', 'Host: 127.0.0.1:8080', 'Connection: keep-alive']
#-*- coding:utf-8 -*- import socket #空行 from multiprocessing import Process # 设置静态文件根目录 HTML_ROOT_DIR = "./html" #常量全部要大写 #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" # 获取客户端请求数据 request_data = client_socket.recv(1024) print("request_data:",request_data) request_data = request_data.decode("utf-8") request_lines = request_data.splitlines() print(request_lines) for line in request_lines: print(line) # 构造响应数据 # 空一格在加注释 response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = "hello world" response = response_start_line + response_headers + " " +response_body print("response:",response) # 向客户端返回相应数据 client_socket.send(bytes(response,"utf-8")) # 关闭客户端连接 client_socket.close() if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) while True: client_socket, client_address = server_socket.accept() #赋值的时候有空格 print("[%s,%s]用户连接上了"%client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
2.正则匹配
>>> s 'GET /favicon.ico HTTP/1.1' >>> re.match(r"w+ +(/[^ ]*)",s).group(1) '/favicon.ico'
#-*- coding:utf-8 -*- import socket import re #空行 from multiprocessing import Process # 设置静态文件根目录 HTML_ROOT_DIR = "./html" #常量全部要大写 #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" # 获取客户端请求数据 request_data = client_socket.recv(1024) print("request_data:",request_data) request_data = request_data.decode("utf-8") request_lines = request_data.splitlines() print(request_lines) # 解析请求报文 for line in request_lines: print(line) # GET /favicon.ico HTTP/1.1 request_start_line = request_lines[0] # 提取用户请求的文件名 file_name = re.match(r"w+ +(/[^ ]*)", request_start_line).group(1) print(file_name) # 构造响应数据 # 空一格在加注释 response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = "hello world" response = response_start_line + response_headers + " " +response_body print("response:",response) # 向客户端返回相应数据 client_socket.send(bytes(response,"utf-8")) # 关闭客户端连接 client_socket.close() if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) while True: client_socket, client_address = server_socket.accept() #赋值的时候有空格 print("[%s,%s]用户连接上了"%client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
3. 打开文件读取内容,抛出异常
#-*- coding:utf-8 -*- import socket import re #空行 from multiprocessing import Process # 设置静态文件根目录 HTML_ROOT_DIR = "./html" #常量全部要大写 #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" # 获取客户端请求数据 request_data = client_socket.recv(1024) print("request_data:",request_data) request_data = request_data.decode("utf-8") request_lines = request_data.splitlines() print(request_lines) # 解析请求报文 for line in request_lines: print(line) # GET /favicon.ico HTTP/1.1 request_start_line = request_lines[0] # 提取用户请求的文件名 file_name = re.match(r"w+ +(/[^ ]*)", request_start_line).group(1) print(file_name) # 打开文件读取内容 file = open(HTML_ROOT_DIR + file_name,"rb") file_data = file.read() file.close() # 构造响应数据 # 空一格在加注释 response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = file_data response = response_start_line + response_headers + " " + response_body print("response:",response) # 向客户端返回相应数据 client_socket.send(bytes(response,"utf-8")) # 关闭客户端连接 client_socket.close() if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind(("",8080)) server_socket.listen(128) while True: client_socket, client_address = server_socket.accept() #赋值的时候有空格 print("[%s,%s]用户连接上了"%client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
4.抛出文件异常,修改字符编码
- 重用端口
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 修改参数 #级别 socket #重用地址 #1
#-*- coding:utf-8 -*- import socket import re #空行 from multiprocessing import Process # 设置静态文件根目录 HTML_ROOT_DIR = "./html" #常量全部要大写 #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" # 获取客户端请求数据 request_data = client_socket.recv(1024) print("request_data:",request_data) request_data = request_data.decode("utf-8") request_lines = request_data.splitlines() print(request_lines) # 解析请求报文 for line in request_lines: print(line) # GET /favicon.ico HTTP/1.1 request_start_line = request_lines[0] # 提取用户请求的文件名 file_name = re.match(r"w+ +(/[^ ]*)", request_start_line).group(1) print(file_name) # 打开文件读取内容 try : file = open(HTML_ROOT_DIR + file_name,"rb") except IOError: response_start_line = "HTTP/1.1 404 Not found " response_headers = "Server: My server " response_body = "the file is not found" else: file_data = file.read() file.close() # 构造响应数据 # 空一格在加注释 response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = file_data.decode("utf-8") response = response_start_line + response_headers + " " + response_body print("response:",response) # 向客户端返回相应数据 client_socket.send(bytes(response,"utf-8")) # 关闭客户端连接 client_socket.close() if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(("",8080)) server_socket.listen(128) while True: client_socket, client_address = server_socket.accept() #赋值的时候有空格 print("[%s,%s]用户连接上了"%client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket
5.主页优化
if "/" == file_name : # 常量放在等号的左边 file_name = "/index.html"
6.完整版
#-*- coding:utf-8 -*- import socket import re #空行 from multiprocessing import Process # 设置静态文件根目录 HTML_ROOT_DIR = "./html" #常量全部要大写 #函数上面有空行2行 def hand_client(client_socket): """处理客户端请求""" #推荐"""""" # 获取客户端请求数据 request_data = client_socket.recv(1024) print("request_data:",request_data) request_data = request_data.decode("utf-8") request_lines = request_data.splitlines() print(request_lines) # 解析请求报文 for line in request_lines: print(line) # GET /favicon.ico HTTP/1.1 request_start_line = request_lines[0] # 提取用户请求的文件名 file_name = re.match(r"w+ +(/[^ ]*)", request_start_line).group(1) print(file_name) if "/" == file_name : # 常量放在等号的左边 file_name = "/index.html" # 打开文件读取内容 try : file = open(HTML_ROOT_DIR + file_name,"rb") except IOError: response_start_line = "HTTP/1.1 404 Not found " response_headers = "Server: My server " response_body = "the file is not found" else: file_data = file.read() file.close() # 构造响应数据 # 空一格在加注释 response_start_line = "HTTP/1.1 200 OK " response_headers = "Server: My server " response_body = file_data.decode("utf-8") response = response_start_line + response_headers + " " + response_body print("response:",response) # 向客户端返回相应数据 client_socket.send(bytes(response,"utf-8")) # 关闭客户端连接 client_socket.close() if __name__ == "__main__": #AF_INET 是个常量 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(("",8080)) server_socket.listen(128) while True: client_socket, client_address = server_socket.accept() #赋值的时候有空格 print("[%s,%s]用户连接上了"%client_address) hand_client_process = Process(target=hand_client,args=(client_socket,)) #=函数名 不能有空格 hand_client_process.start() client_socket.close() #多进程会复制 client_socket