server端 import socket,os,time,hashlib  server=socket.socket()  server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)#允许重用地址 server.bind(("0.0.0.0",7008))#允许任意IP连接 server.listen() while True:      conn,addr=server.accept()      print("new connect:",addr)      while True:          print("等待新指令...")          data=conn.recv(1024)          if not data:              print("客户端已经断开...")              break          cmd,filename=data.decode().split()          print(filename)          if os.path.isfile(filename):              f=open(filename,'rb')              m=hashlib.md5()              file_size=os.stat(filename).st_size              conn.send(str(file_size).encode())#send file size              conn.recv(1024)#wait for ack              for line in f:                  m.update(line)                  conn.send(line)              print("file md5:",m.hexdigest())              f.close()              conn.send(m.hexdigest().encode())#send md5              print('send done')          else:#如果文件不存在就发送一个错误码,并且重新接收命令              print("download file not exist")              False_ack='file not exist'              conn.send(False_ack.encode())  server.close()

 

client端 import socket,hashlib  client=socket.socket()  client.connect(('192.168.74.20',7008)) while True:      cmd=input("请输入get+文件名:").strip()      if len(cmd)==0:continue      if cmd.startswith("get"):          client.send(cmd.encode())          server_response=client.recv(1024)          if server_response.decode()=="file not exist":              continue#如果文件不存在就重新输入命令          print("接收文件大小为:",server_response.decode(),"字节")          client.send(b"ready ro recive file")          file_total_size=int(server_response.decode())          recived_size=0          filename=cmd.split()[1]          f=open(filename+'.new',"wb")          m=hashlib.md5()          while recived_size
1024:#要收不止一次                 size=1024             else:#最后一次了,剩多少收多少                 size=file_total_size-recived_size                 print("last recive:",size,"字节")             data=client.recv(size)             recived_size+=len(data)             m.update(data)             f.write(data)             # print(file_total_size,received_size)         else:             new_file_md5=m.hexdigest()             print("file recive done\nrecived_size:",recived_size,"字节")             print("file_total_size:",file_total_size,"字节")             f.close()         server_file_md5=client.recv(1024)         print("server file md5:",server_file_md5.decode("utf-8"))         print("recive file md5:",new_file_md5)     else:#如果命令首字母不是get就重新输入命令         continue client.close()