python如何實(shí)現(xiàn)ftp文件傳輸功能-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)python如何實(shí)現(xiàn)ftp文件傳輸功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)專注于未央網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供未央營銷型網(wǎng)站建設(shè),未央網(wǎng)站制作、未央網(wǎng)頁設(shè)計(jì)、未央網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造未央網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供未央網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

本文實(shí)例為大家分享了python實(shí)現(xiàn)ftp文件傳輸?shù)木唧w代碼,具體內(nèi)容如下

主要步驟可以分為以下幾步:

1.讀取文件名
2.檢測文件是否存在
3.打開文件
4.檢測文件大小
5.發(fā)送文件大小和 md5值給客戶端
6.等客戶端確認(rèn)
7.開始邊讀邊發(fā)數(shù)據(jù)

服務(wù)器端代碼:

import socket,os,time
import hashlib
 
server =socket.socket()
server.bind(('0.0.0.0',6666))
server.listen()
print("等待....")
while True:
 conn,addr = server.accept()
 print("new conn:",conn)
 while True:
 data = conn.recv(1024)
 if not data:
 print("client is disconnection")
 break
 cmd,filename = data.decode().split() #記錄指令和文件名
 print(filename)
 #判斷當(dāng)前目錄是否存在該文件,而且必須是文件,而不是目錄
 if os.path.isfile(filename):
 f = open(filename,'rb')
 #m = hashlib.md5() # 創(chuàng)建md5
 file_size = os.stat(filename).st_size #stat() 可以返回文件的大小值
 conn.send((str(file_size)).encode()) # 發(fā)送文件大小
 conn.recv(1024) #等待返回信息
 for line in f:
 # m.updata(line) 
 conn.send(line)
 #print("file md5",m.hexdigest()) #打印md5值
 f.close()

客戶端代碼:

# Author: zjt
import socket
 
client = socket.socket()
 
client.connect(("0.0.0.0",6666))
 
 
while True:
 cmd = input(">>>:").strip()
 
 if len(cmd)==0 :continue
 
 if cmd.startswith("get"):
 client.send(cmd.encode())
 server_response = client.recv(1024)
 print("server response: ",server_response)
 client.send(b"ready to recv file")
 
 # 開始接收文件
 file_total_size = int(server_response.decode())
 received_size = 0 # 記錄接收文件的大小
 filename = cmd.split()[1]
 # 因?yàn)閮蓚€(gè)目錄一致,接收的文件名不能與原文件相同
 f = open(filename+".new","wb")
 while received_size < file_total_size:
 data = client.recv(1024)
 received_size += len(data)
 f.write(data)
 print("total:",file_total_size," present: ",received_size)
 else:
 print("file has received done!")
 f.close()
 
client.close()

用80M的文件傳輸測試,效果如下:

python如何實(shí)現(xiàn)ftp文件傳輸功能

程序升級:

前面的代碼還沒添加md5進(jìn)行驗(yàn)證,現(xiàn)在對代碼進(jìn)行升級

服務(wù)器端代碼:

import socket,os,time
import hashlib
 
server =socket.socket()
server.bind(('0.0.0.0',8888))
server.listen()
print("等待....")
while True:
 conn,addr = server.accept()
 print("new conn:",conn)
 while True:
 data = conn.recv(1024)
 if not data:
 print("client is disconnection")
 break
 cmd,filename = data.decode().split() #記錄指令和文件名
 print(filename)
 #判斷當(dāng)前目錄是否存在該文件,而且必須是文件,而不是目錄
 if os.path.isfile(filename):
 f = open(filename,'rb')
 m = hashlib.md5() # 創(chuàng)建md5
 file_size = os.stat(filename).st_size #stat() 可以返回文件的大小值
 conn.send((str(file_size)).encode()) # 發(fā)送文件大小
 conn.recv(1024) #等待返回信息
 for line in f:
 m.update(line)
 conn.send(line)
 print("file md5",m.hexdigest()) #打印md5值
 
 f.close()
 conn.send(m.hexdigest().encode()) # 發(fā)送md5
 
 print("我真的已經(jīng)發(fā)過去了",m.hexdigest().encode())
 
 print("send done")
 
server.close()

客戶端代碼:

import socket
import hashlib
client = socket.socket()
client.connect(("0.0.0.0",8888))
while True:
 cmd = input(">>>:").strip()
 if len(cmd)==0 :continue
 if cmd.startswith("get"):
 client.send(cmd.encode())
 server_response = client.recv(1024)
 print("server response: ",server_response)
 client.send(b"ready to recv file")
 # 開始接收文件
 file_total_size = int(server_response.decode())
 received_size = 0 # 記錄接收文件的大小
 filename = cmd.split()[1]
 # 因?yàn)閮蓚€(gè)目錄一致,接收的文件名不能與原文件相同
 f = open(filename+".new","wb")
 m = hashlib.md5()
 while received_size < file_total_size:
 data = client.recv(1024)
 received_size += len(data)
 m.update(data)
 f.write(data)
 #print("total:",file_total_size," present: ",received_size)
 else:
 new_file_md5 = m.hexdigest()
 print("client file md5:",new_file_md5)
 print("file has received done!")
 print("total:",file_total_size," present: ",received_size)
 f.close()
 sever_file_md5 = client.recv(1024)
 print("client file md5:",new_file_md5)
 print("server file md5:",sever_file_md5)
client.close()

兩個(gè)程序在linux 環(huán)境下運(yùn)行,結(jié)果如下:

python如何實(shí)現(xiàn)ftp文件傳輸功能

可以看到傳輸后文件大小變大了一點(diǎn)點(diǎn),而且md5前后值也不同,說明文件傳輸發(fā)生了改變。

現(xiàn)在講程序在windows環(huán)境下運(yùn)行,結(jié)果如下:

python如何實(shí)現(xiàn)ftp文件傳輸功能

此時(shí)可以看到windows上沒有問題,文件大小相同,且md5值也一致。

原因分析:

之所以會發(fā)生這種情況,是因?yàn)樵趌inux上運(yùn)行時(shí),最后一次傳輸文件與發(fā)送md5值的時(shí)候,發(fā)生可粘包,導(dǎo)致最后一次接收文件的時(shí)候,連同md5的數(shù)據(jù)一并發(fā)送了。而客戶端也當(dāng)作一條接收信息,全部接收了。所以客戶端出現(xiàn)沒有收到來自服務(wù)器端的md5值,多出來的那一點(diǎn)點(diǎn),就是md5值的大小。

解決方法:

在接收文件的時(shí)候,判斷當(dāng)前剩余多少文件需要接收,如果大于1024,就接收1024大小的文件,否則就只接收剩下全部的文件,防止最后一次接收多余的數(shù)據(jù)。

只需要對客戶端代碼進(jìn)行修改,修改后代碼如下:

import socket
import hashlib
client = socket.socket()
client.connect(("0.0.0.0",8888))
while True:
 cmd = input(">>>:").strip()
 if len(cmd)==0 :continue
 if cmd.startswith("get"):
 client.send(cmd.encode())
 server_response = client.recv(1024)
 print("server response: ",server_response)
 client.send(b"ready to recv file")
 # 開始接收文件
 file_total_size = int(server_response.decode())
 received_size = 0 # 記錄接收文件的大小
 filename = cmd.split()[1]
 f = open(filename+".new","wb")
 m = hashlib.md5()
 while received_size < file_total_size:
 #添加一次判斷,使最后一次剩多少就接收多少,避免發(fā)生粘包
 if file_total_size - received_size > 1024:
  size = 1024
 else: # 最后一次,剩多少收多少
  size = file_total_size - received_size
 data = client.recv(size)
 received_size += len(data)
 m.update(data)
 f.write(data)
 else:
 new_file_md5 = m.hexdigest() 
 print("client file md5:",new_file_md5)
 print("file has received done!")
 print("total:",file_total_size," present: ",received_size)
 print("下一句關(guān)閉文件")
 f.close()
 print("開始接收md5 ") 
 sever_file_md5 = client.recv(1024)
 print("client file md5:",new_file_md5)
 print("server file md5:",sever_file_md5)
client.close()

關(guān)于“python如何實(shí)現(xiàn)ftp文件傳輸功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

名稱欄目:python如何實(shí)現(xiàn)ftp文件傳輸功能-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article38/dchjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、服務(wù)器托管、App開發(fā)、商城網(wǎng)站品牌網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營