import socketserver, os , json , struct, hashlib codes = { "文件已存在":1001, "文件不存在":1002 } File_Path = r'C:\Users\17303\Desktop\电脑的组成.avi' #需要传的文件路径 File_Size = os.path.getsize(File_Path) f = open(File_Path,"rb") md5 = hashlib.md5() md5.update(f.read()) md5_val = md5.hexdigest() File_info = { "FileName":"电脑的组成.avi", "FileSize":File_Size, "md5_val":md5_val } """ 先开始 1.发送字典的长度; 2.发送字典 (Filename,文件md5值,FileSize) 3.接收字典 (Code,FileSize)/(Code,FileSize(0)) 4.发送文件 (seek(FIleSize)) 5.校验完整性 """ File_info_json = json.dumps(File_info).encode("utf-8") File_info_json_length = struct.pack("i",len(File_info_json)) class My(socketserver.BaseRequestHandler): def handle(self): self.request.send(File_info_json_length) self.request.send(File_info_json) response = json.loads(self.request.recv(1024).decode("utf-8")) Code = response["Code"] Md5_Size = response["Md5_Size"] self.send_info(File_Path,Md5_Size) client_md5 = self.request.recv(1024).decode("utf-8") self.verify(client_md5,md5_val) def send_info(self, File_Path, Md5_Size): f = open(File_Path,"rb") f.seek(Md5_Size) for line in f: self.request.send(line) def verify(self, client_md5, md5_val): if md5_val == client_md5: self.request.send(b'200') else: self.request.send(b'404') server = socketserver.ThreadingTCPServer(("127.0.0.1",8080,), My) server.serve_forever()