欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于python3實(shí)現(xiàn)socket文件傳輸和校驗(yàn)

 更新時(shí)間:2018年07月28日 11:38:40   作者:m4_Sean  
這篇文章主要為大家詳細(xì)介紹了基于python3實(shí)現(xiàn)socket文件傳輸和校驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

基于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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python3 正則表達(dá)式基礎(chǔ)廖雪峰

    python3 正則表達(dá)式基礎(chǔ)廖雪峰

    正則表達(dá)式是一種用來(lái)匹配字符串的強(qiáng)有力的武器。它的設(shè)計(jì)思想是用一種描述性的語(yǔ)言來(lái)給字符串定義一個(gè)規(guī)則,凡是符合規(guī)則的字符串,我們就認(rèn)為它“匹配”了,否則,該字符串就是不合法的
    2020-03-03
  • python數(shù)據(jù)解析之XPath詳解

    python數(shù)據(jù)解析之XPath詳解

    本篇文章主要介紹了python數(shù)據(jù)解析之xpath的基本使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-09-09
  • 在Python中利用pickle保存變量的實(shí)例

    在Python中利用pickle保存變量的實(shí)例

    今天小編就為大家分享一篇在Python中利用pickle保存變量的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python數(shù)據(jù)分析之堆疊數(shù)組函數(shù)示例總結(jié)

    Python數(shù)據(jù)分析之堆疊數(shù)組函數(shù)示例總結(jié)

    這篇文章主要為大家介紹了Python數(shù)據(jù)分析之堆疊數(shù)組函數(shù)示例總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Python實(shí)現(xiàn)郵件的批量發(fā)送的示例代碼

    Python實(shí)現(xiàn)郵件的批量發(fā)送的示例代碼

    下面小編就為大家分享一篇Python實(shí)現(xiàn)郵件的批量發(fā)送的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Python下簡(jiǎn)易的單例模式詳解

    Python下簡(jiǎn)易的單例模式詳解

    這篇文章主要介紹了Python下簡(jiǎn)易的單例模式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • python3 Scrapy爬蟲(chóng)框架ip代理配置的方法

    python3 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í)例詳解

    今天小編就為大家分享一篇對(duì)django xadmin自定義菜單的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Pytorch多GPU訓(xùn)練過(guò)程

    Pytorch多GPU訓(xùn)練過(guò)程

    這篇文章主要介紹了Pytorch多GPU訓(xùn)練過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 詳解Python中最常用的10個(gè)內(nèi)置函數(shù)

    詳解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

最新評(píng)論