成都网站建设设计

将想法与焦点和您一起共享

socket_ftp_上传下载-创新互联

#main.py
importsocketserver,json,os,hashlib
classMyTcpHandler(socketserver.BaseRequestHandler):
   defput(self,*args):
       cmd_dic=args[0]
       filename=cmd_dic["filename"]
       filesize=cmd_dic["size"]
       ifos.path.isfile(filename):
           f=open(filename+".new",'wb')
       else:
           f=open(filename,'wb')
       self.request.send(b'200 ok')
       received_size=0
       m=hashlib.md5()
       whilereceived_size           data=self.request.recv(1024)
           m.update(data)
           f.write(data)
           received_size+=len(data)
       else:
           uploaded_file_md5=m.hexdigest()
           self.request.send(b"ready recive your md5")
           client_file_md5=self.request.recv(1024)
           print("client_file_md5:",client_file_md5.decode())
           print("uploaded_file_md5:",uploaded_file_md5)
           print("file [%s] has uploaded..."%filename)
   defget(self,*args):
       cmd_dic=args[0]
       filename=cmd_dic["filename"]
       ifos.path.isfile(filename):
           f=open(filename,'rb')
           filesize=os.stat(filename).st_size
           self.request.send(str(filesize).encode())
           self.request.recv(1024)
           m = hashlib.md5()
           forline inf:
               m.update(line)
               self.request.send(line)
           else:
               print("file md5:", m.hexdigest())
               self.request.recv(1024)
               self.request.send(m.hexdigest().encode("utf-8"))
               print("file download success...")
               f.close()
       else:
           print(filename,"is not exist")
   defhandle(self):
       while True:
           try:
               self.data=self.request.recv(1024).strip()
               print("{} wrote:".format(self.client_address[0]))
               print(self.data.decode())
               cmd_dic=json.loads(self.data.decode())
               print("cmd_dic:",cmd_dic)
               action=cmd_dic["action"]
               ifhasattr(self,action):
                   func=(getattr(self,action))
                   func(cmd_dic)
           exceptConnectionResetError ase:
               print("error",e)
               break
if
__name__=="__main__":
   HOST,PORT="localhost",5566
   server=socketserver.ThreadingTCPServer((HOST,PORT),MyTcpHandler)
   server.serve_forever()
   server.allow_reuse_address()

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、虚拟空间、营销软件、网站建设、上犹网站维护、网站推广。
#client.py
importsocket,os,json,hashlib
classFtpclient(object):
   def__init__(self):
       self.client=socket.socket()
   defhelp(self):
       print("put/get/cd/pwd")
   defconnect(self,ip,port):
       self.client.connect((ip,port))
   definteractive(self):
       while True:
           cmd=input("请输入要上传下载的文件:").strip()
           iflen(cmd)==0:continue
cmd_str=cmd.split()[0]
           ifhasattr(self,cmd_str):
               func=getattr(self,cmd_str)
               func(cmd)
           else:
               self.help()
   defput(self,*args):
       cmd_split=args[0].split()
       iflen(cmd_split)>1:
           filename=cmd_split[1]
           ifos.path.isfile(filename):
               filesize=os.stat(filename).st_size
               msg_dic={
                   "action":"put",
                   "filename":filename,
                   "size":filesize,
                   "overridden":True
}
               self.client.send(json.dumps(msg_dic).encode("utf-8"))
               server_response=self.client.recv(1024)
               m=hashlib.md5()
               f=open(filename,'rb')
               forline inf:
                   m.update(line)
                   self.client.send(line)
               else:
                   print("file md5:", m.hexdigest())
                   self.client.recv(1024)
                   self.client.send(m.hexdigest().encode("utf-8"))
                   print("file upload success...")
                   f.close()
           else:
               print(filename,"is not exist")
   defget(self,*args):
       cmd_split=args[0].split()
       iflen(cmd_split)>1:
           filename=cmd_split[1]
           msg_dic={
                   "action":"get",
                   "filename":filename,
                   "overridden":True
}
           self.client.send(json.dumps(msg_dic).encode("utf-8"))
           received_size = 0
           m = hashlib.md5()
           f=open(filename+".download",'wb')
           server_response=self.client.recv(1024)
           file_total_size=int(server_response.decode())
           self.client.send(b"ready to recv file")
           whilereceived_size < file_total_size:
               iffile_total_size - received_size > 1024:
                   size=1024
               else:
                   size=file_total_size - received_size
               data = self.client.recv(size)
               m.update(data)
               f.write(data)
               received_size += len(data)
           else:
               download_file_md5 = m.hexdigest()
               self.client.send(b"ready recive your md5")
               servrer_file_md5 = self.client.recv(1024)
               print("server_file_md5:",servrer_file_md5.decode())
               print("download_file_md5:",download_file_md5)
               print("file [%s] has download..."%filename)
               print("源文件和下载文件大小分别为:",received_size,file_total_size)
ftp=Ftpclient()
ftp.connect("localhost",5566)
ftp.interactive()

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:socket_ftp_上传下载-创新互联
链接分享:http://chengdu.cdxwcx.cn/article/csidod.html