基于python3實(shí)現(xiàn)socket文件傳輸和校驗(yàn)
基于socket的文件傳輸并進(jìn)行MD5值校驗(yàn),供大家參考,具體內(nèi)容如下
文件傳輸分為兩個(gè)類(lèi),一個(gè)是服務(wù)端,一個(gè)是客戶(hù)端。
客戶(hù)端發(fā)起發(fā)送文件或接收文件的請(qǐng)求,服務(wù)端收到請(qǐng)求后接收或發(fā)送文件,最后進(jìn)行MD5值的校驗(yàn)
socket數(shù)據(jù)通過(guò)struct模塊打包
需要發(fā)送文件到服務(wù)端時(shí),調(diào)用sendFile函數(shù),struct包內(nèi)包含文件信息、文件大小、文件MD5等信息,服務(wù)端接收到文件后進(jìn)行MD5值校驗(yàn),校驗(yàn)成功后則返回成功
需要從服務(wù)器下載文件時(shí),調(diào)用recvFile函數(shù),收到文件后進(jìn)行MD5校驗(yàn)
client類(lèi)代碼如下
import socket import struct,os import subprocess dataFormat='8s32s100s100sl' class fileClient(): def __init__(self,addr): self.addr = addr self.action = '' self.fileName = '' self.md5sum = '' self.clientfilePath = '' self.serverfilePath = '' self.size = 0 def struct_pack(self): ret = struct.pack(dataFormat,self.action.encode(),self.md5sum.encode(),self.clientfilePath.encode(), self.serverfilePath.encode(),self.size) return ret def struct_unpack(self,package): self.action,self.md5sum,self.clientfilePath,self.serverfilePath,self.size = struct.unpack(dataFormat,package) self.action = self.action.decode().strip('\x00') self.md5sum = self.md5sum.decode().strip('\x00') self.clientfilePath = self.clientfilePath.decode().strip('\x00') self.serverfilePath = self.serverfilePath.decode().strip('\x00') def sendFile(self,clientfile,serverfile): if not os.path.exists(clientfile): print('源文件/文件夾不存在') return "No such file or directory" self.action = 'upload' (status, output) = subprocess.getstatusoutput("md5sum " + clientfile + " | awk '{printf $1}'") if status == 0: self.md5sum = output else: return "md5sum error:"+status self.size = os.stat(clientfile).st_size self.serverfilePath = serverfile self.clientfilePath = clientfile ret = self.struct_pack() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(self.addr) s.send(ret) recv = s.recv(1024) if recv.decode() == 'dirNotExist': print("目標(biāo)文件/文件夾不存在") return "No such file or directory" elif recv.decode() == 'ok': fo = open(clientfile, 'rb') while True: filedata = fo.read(1024) if not filedata: break s.send(filedata) fo.close() recv = s.recv(1024) if recv.decode() == 'ok': print("文件傳輸成功") s.close() return 0 else: s.close() return "md5sum error:md5sum is not correct!" except Exception as e: print(e) return "error:"+str(e) def recvFile(self,clientfile,serverfile): if not os.path.isdir(clientfile): filePath,fileName = os.path.split(clientfile) else: filePath = clientfile if not os.path.exists(filePath): print('本地目標(biāo)文件/文件夾不存在') return "No such file or directory" self.action = 'download' self.clientfilePath = clientfile self.serverfilePath = serverfile s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(self.addr) ret = self.struct_pack() s.send(ret) recv = s.recv(struct.calcsize(dataFormat)) self.struct_unpack(recv) if self.action.startswith("ok"): if os.path.isdir(clientfile): fileName = (os.path.split(serverfile))[1] clientfile = os.path.join(clientfile, fileName) self.recvd_size = 0 file = open(clientfile, 'wb') while not self.recvd_size == self.size: if self.size - self.recvd_size > 1024: rdata = s.recv(1024) self.recvd_size += len(rdata) else: rdata = s.recv(self.size - self.recvd_size) self.recvd_size = self.size file.write(rdata) file.close() print('\n等待校驗(yàn)...') (status, output) = subprocess.getstatusoutput("md5sum " + clientfile + " | awk '{printf $1}'") if output == self.md5sum: print("文件傳輸成功") else: print("文件校驗(yàn)不通過(guò)") (status, output) = subprocess.getstatusoutput("rm " + clientfile) elif self.action.startswith("nofile"): print('遠(yuǎn)程源文件/文件夾不存在') return "No such file or directory" except Exception as e: print(e) return "error:"+str(e)
server類(lèi)代碼如下
import socket import struct,os import subprocess import socketserver dataFormat='8s32s100s100sl' class fileServer(socketserver.StreamRequestHandler): def struct_pack(self): ret = struct.pack(dataFormat, self.action.encode(), self.md5sum.encode(), self.clientfilePath.encode(), self.serverfilePath.encode(), self.size) return ret def struct_unpack(self, package): self.action, self.md5sum, self.clientfilePath, self.serverfilePath, self.size = struct.unpack(dataFormat, package) self.action = self.action.decode().strip('\x00') self.md5sum = self.md5sum.decode().strip('\x00') self.clientfilePath = self.clientfilePath.decode().strip('\x00') self.serverfilePath = self.serverfilePath.decode().strip('\x00') def handle(self): print('connected from:', self.client_address) fileinfo_size = struct.calcsize(dataFormat) self.buf = self.request.recv(fileinfo_size) if self.buf: self.struct_unpack(self.buf) print("get action:"+self.action) if self.action.startswith("upload"): try: if os.path.isdir(self.serverfilePath): fileName = (os.path.split(self.clientfilePath))[1] self.serverfilePath = os.path.join(self.serverfilePath, fileName) filePath,fileName = os.path.split(self.serverfilePath) if not os.path.exists(filePath): self.request.send(str.encode('dirNotExist')) else: self.request.send(str.encode('ok')) recvd_size = 0 file = open(self.serverfilePath, 'wb') while not recvd_size == self.size: if self.size - recvd_size > 1024: rdata = self.request.recv(1024) recvd_size += len(rdata) else: rdata = self.request.recv(self.size - recvd_size) recvd_size = self.size file.write(rdata) file.close() (status, output) = subprocess.getstatusoutput("md5sum " + self.serverfilePath + " | awk '{printf $1}'") if output == self.md5sum: self.request.send(str.encode('ok')) else: self.request.send(str.encode('md5sum error')) except Exception as e: print(e) finally: self.request.close() elif self.action.startswith("download"): try: if os.path.exists(self.serverfilePath): (status, output) = subprocess.getstatusoutput("md5sum " + self.serverfilePath + " | awk '{printf $1}'") if status == 0: self.md5sum = output self.action = 'ok' self.size = os.stat(self.serverfilePath).st_size ret = self.struct_pack() self.request.send(ret) fo = open(self.serverfilePath, 'rb') while True: filedata = fo.read(1024) if not filedata: break self.request.send(filedata) fo.close() else: self.action = 'nofile' ret = self.struct_pack() self.request.send(ret) except Exception as e: print(e) finally: self.request.close()
調(diào)用server,并開(kāi)啟服務(wù)
import fileSocket import threading import socketserver import time serverIp = '127.0.0.1' serverPort = 19821 serverAddr = (serverIp,serverPort) class fileServerth(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.create_time = time.time() self.local = threading.local() def run(self): print("fileServer is running...") fileserver.serve_forever() fileserver = socketserver.ThreadingTCPServer(serverAddr, fileSocket.fileServer) fileserverth = fileServerth() fileserverth.start()
調(diào)用client,發(fā)送/接受文件
import fileSocket serverIp = '127.0.0.1' serverPort = 19821 serverAddr = (serverIp,serverPort) fileclient = fileSocket.fileClient(serverAddr) fileclient.sendFile('fromClientPath/file','toServerPath/file') fileclient.recvFile('toClientPath/file','fromServerPath/file')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python3使用TCP編寫(xiě)一個(gè)簡(jiǎn)易的文件下載器功能
- python:socket傳輸大文件示例
- Python Socket傳輸文件示例
- python 通過(guò) socket 發(fā)送文件的實(shí)例代碼
- python 使用socket傳輸圖片視頻等文件的實(shí)現(xiàn)方式
- 樹(shù)莓派采用socket方式文件傳輸(python)
- 使用python socket分發(fā)大文件的實(shí)現(xiàn)方法
- Python socket模塊ftp傳輸文件過(guò)程解析
- python socket 聊天室實(shí)例代碼詳解
- Python實(shí)現(xiàn)socket非阻塞通訊功能示例
- Python socket實(shí)現(xiàn)的文件下載器功能示例
相關(guān)文章
Python數(shù)據(jù)分析之堆疊數(shù)組函數(shù)示例總結(jié)
這篇文章主要為大家介紹了Python數(shù)據(jù)分析之堆疊數(shù)組函數(shù)示例總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Python實(shí)現(xiàn)郵件的批量發(fā)送的示例代碼
下面小編就為大家分享一篇Python實(shí)現(xiàn)郵件的批量發(fā)送的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01python3 Scrapy爬蟲(chóng)框架ip代理配置的方法
Scrapy是用python實(shí)現(xiàn)的一個(gè)為了爬取網(wǎng)站數(shù)據(jù),提取結(jié)構(gòu)性數(shù)據(jù)而編寫(xiě)的應(yīng)用框架。使用Twisted高效異步網(wǎng)絡(luò)框架來(lái)處理網(wǎng)絡(luò)通信。這篇文章主要介紹了python3 Scrapy爬蟲(chóng)框架ip代理配置,需要的朋友可以參考下2020-01-01對(duì)django xadmin自定義菜單的實(shí)例詳解
今天小編就為大家分享一篇對(duì)django xadmin自定義菜單的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01詳解Python中最常用的10個(gè)內(nèi)置函數(shù)
Python作為一種多用途編程語(yǔ)言,擁有豐富的內(nèi)置函數(shù)庫(kù),這些函數(shù)可以極大地提高開(kāi)發(fā)效率,本文將介紹Python中最常用的10個(gè)內(nèi)置函數(shù),我們將深入了解每個(gè)函數(shù),并提供示例代碼以幫助您更好地理解它們,需要的朋友可以參考下2023-11-11