python實(shí)現(xiàn)ftp文件傳輸系統(tǒng)(案例分析)
最近做了一個(gè)簡單的文件傳輸系統(tǒng),基于ftp協(xié)議,使用python語言開發(fā),雖然python里面已經(jīng)有ftplib模塊,可以很容易的實(shí)現(xiàn)ftp服務(wù)器。這次我使用的是socket實(shí)現(xiàn)client與ftp server之間的通訊和文件傳輸,client另起一個(gè)flask服務(wù)器,用于用戶在瀏覽器端的交互。系統(tǒng)實(shí)現(xiàn)的功能有:用戶登錄注冊(cè),用戶查看ftp服務(wù)器端文件和下載上傳刪除操作,支持多進(jìn)程、多用戶。
一,登錄注冊(cè)
該項(xiàng)目使用的是mongo數(shù)據(jù)庫,其實(shí)用戶登錄注冊(cè)功能很好實(shí)現(xiàn),沒有什么技術(shù)細(xì)節(jié),這里就略過了。數(shù)據(jù)庫管理代碼如下:
import pymongofrom pymongo.collection import Collectionclass DBManager(object): def __init__(self): client = pymongo.MongoClient('mongodb://localhost:27017/') self.db = client['FTPDB'] self.users = self.db[’user’] #保存用戶登錄信息 def saveUserInfo(self,account,password): users = self.users.find() for item in users: accountDB = item[’account’] if accountDB == account: return 'false' data = [] userInfo = {} userInfo[’account’] = account userInfo[’password’] = password data.append(userInfo) collection = Collection(self.db,'user') collection.insert(data) return 'true' def confirmUserLoginInfo(self,account,password): users = self.users.find() ’’’ result狀態(tài): 1:表示初始狀態(tài),即不存在用戶 2:表示存在該用戶、密碼不正確 3:驗(yàn)證成功 ’’’ result = 1 for item in users: accountDB = item[’account’] passwordDB = item[’password’] if accountDB == account: if passwordDB == password: result = 3 else: result = 2 return result
前端注冊(cè)js代碼如下:
function register() { account = $('#account').val(); password = $('#password').val(); confirmPassword = $('#confirmPassword').val(); if(account == null || password == null || confirmPassword == null){ alert('請(qǐng)先輸入必要信息') return; } if(password != confirmPassword){ alert('密碼不一致'); return; } var request = { type:'register', account:account, password:password } sendData('http://localhost:8080/register',request)}//向服務(wù)器發(fā)送數(shù)據(jù)function sendData(url,json) { $.ajax({ url: url, //請(qǐng)求的url地址 dataType: 'json', //返回格式為json async: true, //請(qǐng)求是否異步,默認(rèn)為異步,這也是ajax重要特性 data: json, //參數(shù)值 type: 'post', //請(qǐng)求方式 success:function(data){ //alert(data) if(data.toString() == 'false'){ alert('用戶名已存在'); }else{ window.location.href = 'http://localhost:8080/index'; } }, error:function (error) { console.log(error); } });}
二,文件管理(文件查看、刪除、上傳、下載)
客戶端與服務(wù)器端約定命令格式,服務(wù)器通過解析客戶端命令來執(zhí)行操作。
server.pyfrom socket import *import os,sysimport signalimport time# 全局變量HOST = ’0.0.0.0’PORT = 8686ADDR = (HOST,PORT)FILE_PATH = ’../serverFiles/’# 處理僵尸進(jìn)程signal.signal(signal.SIGCHLD,signal.SIG_IGN)# 服務(wù)端功能類class Server(object): def __init__(self): self.connfd = '' def do_list(self,account): # 獲取文件列表 file_list = os.listdir(FILE_PATH+account) if not file_list: self.connfd.send('服務(wù)器文件庫為空'.encode()) return else: self.connfd.send(b'OK') time.sleep(0.1) files = '' for file in file_list: if file[0] != ’.’ and os.path.isfile(FILE_PATH + account +'/'+ file): files += file + ’#’ self.connfd.send(files.encode()) def delete(self,accout,fileName): os.remove(FILE_PATH + accout + '/' + fileName) self.connfd.send(b'OK') time.sleep(0.1) def do_get(self,account,filename): try: fd = open(FILE_PATH + account +'/'+ filename,’rb’) except IOError: self.connfd.send('文件不存在'.encode()) return else: #print('發(fā)送OK') self.connfd.send(b’OK’) time.sleep(0.1) # 發(fā)送文件內(nèi)容 while True: data = fd.read(1024) if not data: time.sleep(0.1) self.connfd.send(b’##’) break #print('正在發(fā)送數(shù)據(jù)') self.connfd.send(data) fd.close() def do_put(self,account,filename): if os.path.exists(FILE_PATH + account +'/'+ filename): self.connfd.send(’該文件已存在’.encode()) return fd = open(FILE_PATH + account +'/'+ filename,’wb’) self.connfd.send(b’OK’) # 接收文件內(nèi)容 while True: data = self.connfd.recv(1024) if data == b’**’: break fd.write(data) fd.close() def socket_tcp(self): s = socket() s.setsockopt(SOL_SOCKET,SO_REUSEADDR,True) s.bind(ADDR) s.listen(5) print('Listen the port 8686...') return s def do_request(self,connfd): self.connfd = connfd while True: data = connfd.recv(1024).decode() datas = data.split(’ ’) if not data or datas[1] == ’QUIT@#’: connfd.close() return elif datas[1] == 'LIST@#': #print('list') self.do_list(datas[0]) elif datas[1] == ’GET@#’: filename = datas[-1] self.do_get(datas[0],filename) elif datas[1] == ’PUT@#’: filename = datas[-1] self.do_put(datas[0],filename) elif datas[1] == ’delete@#’: filename = datas[-1] self.delete(datas[0],filename) def run(self): # 創(chuàng)建套接字 s = self.socket_tcp() while True: try: connfd,addr = s.accept() except KeyboardInterrupt: sys.exit('服務(wù)器退出') except Exception as e: print(e) continue print('Connect from',addr) # 創(chuàng)建子進(jìn)程 pid = os.fork() if pid == 0: s.close() self.do_request(connfd) #處理客戶端具體請(qǐng)求 os._exit(0) else: connfd.close()if __name__ == '__main__': server = Server() server.run()client.py''' client.py'''import socketimport os,sysimport time# 服務(wù)器地址ADDR = ('127.0.0.1',8686)FILE_PATH = './clientFiles/'# 客戶端功能類class Client(object): def __init__(self,account): self.sockfd = '' self.account = account #獲得服務(wù)器文件列表 def server_list(self): ftpServerFiles = [] self.sockfd.send((self.account+’ LIST@# ’).encode()) # 等待回復(fù) data = self.sockfd.recv(128).decode() if data == 'OK': files = self.sockfd.recv(4096).decode() for file in files.split(’#’): #print(file) ftpServerFiles.append(file) else: # 無法完成操作 print(data) ftpServerFiles = ftpServerFiles[:-1] return ftpServerFiles #獲得用戶文件夾列表 def client_list(self): # 獲取文件列表 userFiles = [] file_list = os.listdir(FILE_PATH+self.account+'/') if not file_list: return else: time.sleep(0.1) files = '' for file in file_list: if file[0] != ’.’ and os.path.isfile(FILE_PATH + self.account + '/' + file): userFiles.append(file) return userFiles #退出 def do_quit(self): self.sockfd.send((self.account+’ QUIT@# ’).encode()) self.sockfd.close() sys.exit(’謝謝使用’) #用戶下載服務(wù)器文件 def do_get(self,filename): self.sockfd.send((self.account+’ GET@# ’+filename).encode()) data = self.sockfd.recv(128).decode() if data == ’OK’: fd = open(FILE_PATH + self.account + '/' + filename,’wb’) #復(fù)寫 while True: data = self.sockfd.recv(1024) if data == b’##’: #print('##') break #print('正在寫入數(shù)據(jù)') fd.write(data) fd.close() else: print(data) #用戶將文件上傳到服務(wù)器文件夾 def do_put(self,filename): try: fd = open(FILE_PATH + self.account + '/' + filename,’rb’) except IOError: print('文件不存在') return # 獲取文件名 filename = filename.split(’/’)[-1] # else: self.sockfd.send((self.account+’ PUT@# ’+filename).encode()) data = self.sockfd.recv(128).decode() # 發(fā)送文件 if data == ’OK’: while True: data = fd.read(1024) if not data: time.sleep(0.1) self.sockfd.send(b’**’) break self.sockfd.send(data) fd.close() return 'true' else: print(data) return 'false' #刪除用戶文件 def removeU(self,fileName): os.remove(FILE_PATH + self.account + '/' + fileName) return 'true' #刪除用戶文件 def removeF(self,fileName): self.sockfd.send((self.account+’ delete@# ’+fileName).encode()) # 等待回復(fù) data = self.sockfd.recv(128).decode() if data == 'OK': return 'true' def menu_display(self): print('n------命令選擇-------') print('*** clist ***') print('*** slist ***') print('*** get list ***') print('*** put list ***') print('*** quit ***') print('----------------------') def run(self,cmd): # 創(chuàng)建套接字 sockfd = socket.socket() try: sockfd.connect(ADDR) except Exception as e: print(e) return result = '' self.sockfd = sockfd # choice(cmd,ftp) if cmd == 'slist': result = self.server_list() return result elif cmd == 'clist': result = self.client_list() return result elif cmd ==’quit’: self.do_quit() elif cmd[:3] == ’get’: filename = cmd.strip().split(’ ’)[-1] self.do_get(filename) elif cmd[:3] == ’put’: filename = cmd.strip().split(’ ’)[-1] result = self.do_put(filename) return result elif cmd[:7] == ’removeU’: filename = cmd.strip().split(’ ’)[-1] self.removeU(filename) elif cmd[:7] == ’removeF’: filename = cmd.strip().split(’ ’)[-1] self.removeF(filename) else: print('請(qǐng)輸入正確命令!')if __name__ == '__main__': client = Client('ffy') client.run()
運(yùn)行界面:
總結(jié)
到此這篇關(guān)于python實(shí)現(xiàn)ftp文件傳輸系統(tǒng)的文章就介紹到這了,更多相關(guān)python ftp文件傳輸內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介4. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. SharePoint Server 2019新特性介紹7. React+umi+typeScript創(chuàng)建項(xiàng)目的過程8. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析10. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究
