python實(shí)現(xiàn)FTP文件傳輸?shù)姆椒ǎǚ?wù)器端和客戶端)
用python實(shí)現(xiàn)FTP文件傳輸,包括服務(wù)器端和客戶端,要求
(1)客戶端訪問(wèn)服務(wù)器端要有一個(gè)驗(yàn)證功能
(2)可以有多個(gè)客戶端訪問(wèn)服務(wù)器端
(3)可以對(duì)重名文件重新上傳或下載
FTP(File Transfer Protocol,文件傳輸協(xié)議) 是 TCP/IP 協(xié)議組中的協(xié)議之一。FTP協(xié)議包括兩個(gè)組成部分,其一為FTP服務(wù)器,其二為FTP客戶端。其中FTP服務(wù)器用來(lái)存儲(chǔ)文件,用戶可以使用FTP客戶端通過(guò)FTP協(xié)議訪問(wèn)位于FTP服務(wù)器上的資源。在開(kāi)發(fā)網(wǎng)站的時(shí)候,通常利用FTP協(xié)議把網(wǎng)頁(yè)或程序傳到Web服務(wù)器上。它工作在TCP 模型的第四層, 即應(yīng)用層, 使用 TCP 傳輸而不是 UDP, 客戶在和服務(wù)器建立連接前要經(jīng)過(guò)一個(gè)“三次握手”的過(guò)程, 保證客戶與服務(wù)器之間的連接是可靠的, 而且是面向連接, 為數(shù)據(jù)傳輸提供可靠保證。
服務(wù)器端
首先要實(shí)現(xiàn)對(duì)訪問(wèn)客戶端的驗(yàn)證,在本地建立一個(gè)數(shù)據(jù)庫(kù)文件,將客戶端的用戶名和密碼寫(xiě)入到文件中。這樣每次訪問(wèn)時(shí)都將用戶名和密碼和數(shù)據(jù)庫(kù)中存在的進(jìn)行匹配,實(shí)現(xiàn)驗(yàn)證功能。這里對(duì)密碼進(jìn)行了MD5加密,保證了密碼不會(huì)輕易泄露。
{'username': 'ahpu', 'password': '96e79218965eb72c92a549dd5a330112', 'limitsize': 10240000, 'homepath': 'D:FTPhomeahpu'}
登錄驗(yàn)證功能具體實(shí)現(xiàn)
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Author : hghimport hashlibimport osimport jsonfrom conf import settingsclass User_auth(object): def auth(self, account_info): ''' #此功能是進(jìn)行用戶的登錄信息驗(yàn)證,如果登錄成功,那么返回用戶對(duì)應(yīng)的http狀態(tài)碼及賬戶信息,否則只返回http狀態(tài)碼 :param account_info: 用戶的賬戶信息:用戶名,密碼 :return: ''' name = account_info.split(':')[0] pwd = account_info.split(':')[1] pwd = self.hash(pwd.encode()) # 將用戶名的密碼轉(zhuǎn)換成hash值 user_db_file = settings.DATABASE + r'%s.db' % name # 也可以寫(xiě)成 '%s.db' or '/%s.db' if os.path.isfile(user_db_file): # 輸入的用戶名存在 with open(user_db_file) as fr: user_db_info = json.loads(fr.read()) # or josn.load(fr) if pwd == user_db_info[’password’]: return '200', user_db_info # 確定,客戶請(qǐng)求成功 else: return '403.11', None # 密碼錯(cuò)誤 else: return '400', None # 用戶名不存在,用戶認(rèn)證失敗 def hash(self, pwd): ''' 用戶的密碼加密 :param self: :param pwd: 用戶密碼 :return: ''' m = hashlib.md5() m.update(pwd) return m.hexdigest()
然后是重傳功能實(shí)現(xiàn)
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Author : hghimport hashlibimport sysclass Breakpoint(object): # 本模塊確認(rèn)用戶上傳或下載的文件是否存在,如果存在是否需要斷點(diǎn)續(xù)傳 def transfer(self, filename, has_send_size, total_size, conn): ''' 進(jìn)行續(xù)傳 :param filename: :param has_send_size: 已經(jīng)發(fā)送的文件大小 :param total_size: 需要傳輸文件總大小 :param conn: 客戶端和服務(wù)端進(jìn)行數(shù)據(jù)交換的接口 :return: ''' with open(filename, ’rb’) as fr: fr.seek(has_send_size) # 定位到續(xù)傳的位置 print('has_send', has_send_size, 'total', total_size) m = hashlib.md5() if has_send_size == total_size: self.progress_bar(has_send_size, total_size) for line in fr: conn.send(line) m.update(line) has_send_size += len(line) # self.progress_bar(has_send_size,total_size) return m.hexdigest() def progress_bar(self, has_send_size, total_size): bar_width = 50 # 進(jìn)度條長(zhǎng)度 process = has_send_size / total_size send_bar = int(process * bar_width + 0.5) # 發(fā)送的數(shù)據(jù)占到的進(jìn)度條長(zhǎng)度,四舍五入取整 sys.stdout.write('#' * send_bar + '=' * (bar_width - send_bar) + 'r') # 注意點(diǎn):只能這么寫(xiě)才能達(dá)到要求 sys.stdout.write('r%.2f%%: %s%s' % (process * 100, '#' * send_bar, '=' * (bar_width - send_bar))) # 注意點(diǎn):在pycharm中要加rn # 用sublime只要r否則換行 sys.stdout.flush()
服務(wù)器端代碼
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Author : hghimport sysimport osfrom core import socket_serverpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.path.append(path)if __name__ == '__main__': HOST, PORT = '192.168.40.1', 9901 server = socket_server.socketserver.ThreadingTCPServer((HOST, PORT), socket_server.MyTCPServer) server.serve_forever()
客戶端
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Author : hghfrom core import socket_clientimport osimport syspath = os.path.dirname(os.path.abspath(__file__))sys.path.append(path)if __name__ == '__main__': host, port = '192.168.40.1', 9901 myClient = socket_client.MySocketClient(host, port) myClient.start()
由于篇幅有限,具體服務(wù)器端及客戶端代碼都放在了github上,地址https://github.com/heguohang/FTP-python
總結(jié)
到此這篇關(guān)于python實(shí)現(xiàn)FTP文件傳輸(服務(wù)器端和客戶端) 的文章就介紹到這了,更多相關(guān)python ftp 文件傳輸內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. html中的form不提交(排除)某些input 原創(chuàng)3. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼4. 網(wǎng)頁(yè)中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)5. CSS3中Transition屬性詳解以及示例分享6. python 如何在 Matplotlib 中繪制垂直線7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼9. jsp文件下載功能實(shí)現(xiàn)代碼10. 開(kāi)發(fā)效率翻倍的Web API使用技巧
