一、Socketserver實(shí)現(xiàn)FTP,文件上傳、下載
創(chuàng)新互聯(lián)建站企業(yè)建站,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),專(zhuān)注于網(wǎng)站建設(shè)技術(shù),精于網(wǎng)頁(yè)設(shè)計(jì),有多年建站和網(wǎng)站代運(yùn)營(yíng)經(jīng)驗(yàn),設(shè)計(jì)師為客戶(hù)打造網(wǎng)絡(luò)企業(yè)風(fēng)格,提供周到的建站售前咨詢(xún)和貼心的售后服務(wù)。對(duì)于成都網(wǎng)站建設(shè)、成都網(wǎng)站制作中不同領(lǐng)域進(jìn)行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設(shè)中充分了解客戶(hù)行業(yè)的需求,以靈動(dòng)的思維在網(wǎng)頁(yè)中充分展現(xiàn),通過(guò)對(duì)客戶(hù)行業(yè)精準(zhǔn)市場(chǎng)調(diào)研,為客戶(hù)提供的解決方案。目錄結(jié)構(gòu)
1、socketserver實(shí)現(xiàn)ftp文件上傳下載,可以同時(shí)多用戶(hù)登錄、上傳、下載
效果圖:
二、上面只演示了下載,上傳也是一樣的,來(lái)不及演示了,上代碼
1、客戶(hù)端
import socket,hashlib,os,json,sys,time class Ftpclient(object): def __init__(self): self.client = socket.socket() def connect(self,ip,port): self.client.connect((ip, port)) def help(self): msg=''' ls pwd cd .. get filename put filename ''' print(msg) def interactive(self): """ 客戶(hù)端入口 :return: """ while True: verify = self.authenticate() #服務(wù)器端認(rèn)證 if verify: while True: cmd = input('輸入命令 >>').strip() if len(cmd) == 0:continue cmd_str = cmd.split()[0] if hasattr(self,'cmd_%s' %cmd_str): func = getattr(self,'cmd_%s' %cmd_str) func(cmd) else: self.help() def cmd_put(self,*args): """ 上傳文件 :param args: :return: """ cmd_solit = args[0].split() start_time = self.alltime() # 開(kāi)始時(shí)間 if len(cmd_solit) > 1: filename = cmd_solit[1] if os.path.isfile(filename): filesize = os.stat(filename).st_size msg_dic = { 'filename':filename, 'size':filesize, 'overridden':True, 'action':cmd_solit[0] } self.client.send( json.dumps(msg_dic).encode('utf-8')) server_respinse=self.client.recv(1024) #防止粘包,等服務(wù)器確認(rèn)返回 print('文件開(kāi)始上傳',server_respinse) client_size = 0 f = open(filename,'rb') for line in f: client_size += self.client.send(line) self.processBar(client_size,filesize) #進(jìn)度條 else: print('文件傳輸完畢,大小為 %s'%client_size) end_time = self.alltime() # 結(jié)束時(shí)間 print('本次上傳花費(fèi)了%s 秒'%self.alltime(end_time,start_time)) f.close() else: print(filename,'文件不存在') else: print('輸入有誤!') def cmd_get(self,*args): """ 下載文件 :param args: :return: """ cmd_solit = args[0].split() start_time = self.alltime() # 開(kāi)始時(shí)間 filename = cmd_solit[1] if len(cmd_solit) > 1: msg_dic = { 'filename': filename, 'size': '', 'overridden': True, 'action': cmd_solit[0], 'file_exist':'' } self.client.send(json.dumps(msg_dic).encode('utf-8')) self.data = self.client.recv(1024).strip() cmd_dic = json.loads(self.data.decode('utf-8')) print(cmd_dic) if cmd_dic['file_exist']: if os.path.isfile(filename): f = open(filename + '.new', 'wb') else: f = open(filename, 'wb') self.client.send(b'200 ok') #防止粘包,等服務(wù)器確認(rèn)返回 client_size = 0 filesize = cmd_dic['size'] m = hashlib.md5() while client_size < filesize: data=self.client.recv(1024) f.write(data) client_size +=len(data) m.update(data) self.processBar(client_size, filesize) else: print('下載完畢') end_time = self.alltime() # 結(jié)束時(shí)間 print('本次下載花費(fèi)了%s 秒' % self.alltime(end_time, start_time)) f.close() new_file_md5 = m.hexdigest() server_file_md5 = self.client.recv(1024) print('MD5', server_file_md5,new_file_md5) else: print('下載的 %s文件不存在'%filename) else: print('輸入有誤!') def cmd_dir(self,*arge): cmd_solit = arge[0].split() if len(cmd_solit) > 0: msg_dic = { 'action': cmd_solit[0] } self.client.send(json.dumps(msg_dic).encode()) cmd_dir = self.client.recv(1024) print(cmd_dir.decode()) else: print('輸入錯(cuò)誤!') def alltime(self,*args): """ 計(jì)算上傳、下載時(shí)間 :param args: :return: """ if args: return round(args[0] - args[1]) else: return time.time() def processBar(self,num, total): """ 進(jìn)度條 :param num:文件總大小 :param total: 已存入文件大小 :return: """ rate = num / total rate_num = int(rate * 100) if rate_num == 100: r = '\r%s>%d%%\n' % ('=' * int(rate_num /3), rate_num,) else: r = '\r%s>%d%%' % ('=' * int(rate_num /3), rate_num,) sys.stdout.write(r) sys.stdout.flush def authenticate(self): """ 用戶(hù)加密認(rèn)證 :return: """ username = input('輸入用戶(hù)名:>>') password = input('輸入密碼:>>') m = hashlib.md5() if len(username) > 0 and len(password) >0: username = ''.join(username.split()) password = ''.join(password.split()) m.update(username.encode('utf-8')) m.update(password.encode('utf-8')) m = { 'username':username, 'password':password, 'md5':m.hexdigest() } self.client.send(json.dumps(m).encode('utf-8')) server_user_md5 = self.client.recv(1024).strip() print(server_user_md5.decode()) if server_user_md5.decode() == 'success': print('登錄成功!') return 'ok' else: print('用戶(hù)名密碼錯(cuò)誤!') else: print('請(qǐng)輸入用戶(hù)名密碼') f = Ftpclient() f.connect('localhost',9999) f.interactive()
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。