溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在Python中使用socket模塊傳輸ftp文件

發(fā)布時(shí)間:2021-03-18 16:02:18 來源:億速云 閱讀:266 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)如何在Python中使用socket模塊傳輸ftp文件,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

使用環(huán)境:python3,window環(huán)境,需要在頭部聲明# -*- coding:utf-8 -*-

實(shí)現(xiàn)功能:

將sever端所處文件夾的文件,傳輸?shù)絚lient端所處的文件夾中。

并且通過md5檢測(cè)是否出錯(cuò)。

客戶端命令的形式是: get 文件名

client處的新文件是 文件名.new

ftp_sever.py

import hashlib
import socket ,os,time
server = socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
  print("I am waiting for connection.")
  conn, addr = server.accept()
  print("new conn:",addr)
  while True:
    print("等待新指令")
    data = conn.recv(1024)
    if not data:
      print("客戶端已斷開")
      break
    cmd,filename = data.decode().split()
    print(filename)
    if os.path.isfile(filename):  #判斷是否該文件名為文件
      f = open(filename,"rb")
      m = hashlib.md5() #為md5準(zhǔn)備
      file_size = os.stat(filename).st_size #利用os.stat獲取文件的大小
      conn.send( str(file_size).encode() ) #send file size
      conn.recv(1024) #等待確認(rèn),同時(shí)可以防止粘包。
      for line in f: #一行一行發(fā)送數(shù)據(jù),同時(shí)更新md5
       m.update(line)  #不斷更新md5
       conn.send(line) #不斷發(fā)送數(shù)據(jù)。
      print("file md5", m.hexdigest()) #十六進(jìn)制的md5
      f.close()
      conn.send(m.hexdigest().encode()) #send md5
    print("send done")
server.close()

ftp_client.py

import socket
import hashlib

client = socket.socket()

client.connect(('localhost', 9999))

while True:
  cmd = input(">>:").strip() #形式 get filename
  if len(cmd) == 0: continue
  if cmd.startswith("get"):
    client.send(cmd.encode()) #發(fā)送命令,形式 get filename
    server_response = client.recv(1024) #接收文件大小信息
    print("servr response:", server_response)
    client.send(b"ready to recv file") #發(fā)送確認(rèn)信息。
    file_total_size = int(server_response.decode()) #將文件大小int化。
    received_size = 0 #初始化接收數(shù)據(jù)大小,為0
    filename = cmd.split()[1] #獲取文件名
    f = open(filename + ".new", "wb") #以二進(jìn)制形式寫入
    m = hashlib.md5() #為md5準(zhǔn)備

    while received_size != file_total_size:
'''下面的if判斷是用來完整接收文件,從而避免粘包。'''
if file_total_size - received_size > 1024: # 要收不止一次 24 size = 1024 25 else: # 最后一次了,剩多少收多少 26 size = file_total_size - received_size 27 print("last receive:", size) 28 data = client.recv(size) #data只需要是一小個(gè)內(nèi)存,大小為1k就好 29 received_size += len(data) 30 m.update(data) #不斷更新md5 31 f.write(data) #不斷寫入 32 # print(file_total_size,received_size) 33 else: 34 new_file_md5 = m.hexdigest() #獲取十六進(jìn)制的md5 35 print("file recv done", received_size, file_total_size) 36 f.close() 37 server_file_md5 = client.recv(1024) #接收md5值 38 print("server file md5:", server_file_md5) 39 print("client file md5:", new_file_md5) 40 41 client.close()

以上就是如何在Python中使用socket模塊傳輸ftp文件,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI