简体   繁体   中英

MD5 value for file in a remote FTP server using Python ftplib

Once transferring a file to a server using ftplib, how do I determine the MD5 of that file to the one in my local server?

from ftplib import FTP
import hashlib

ftp = FTP('server')
ftp.login('username','password')
ftp.cwd('path_to_upload')

file_to_upload = open(file,'rb') 
filename = os.path.basename(file)
ftp.storbinary('STOR ' + filename, file_to_upload)

local_file_hash = hashlib.md5(open(file, 'rb').read()).hexdigest()
# not sure how to achieve this
server_file_hash = hashlib.md5(open(filename, 'rb').read()).hexdigest() 

if local_file_hash == server_file_hash:
     print("Successful transfer")
else:
     print("Failure transfer")

First, make sure your remote server supports the checksum calculation at all. Many do not. I believe there's even no standard FTP command to calculate a checksum of a remote file. There were many proposals and there are many proprietary solutions.

The latest proposal is:
https://datatracker.ietf.org/doc/html/draft-bryan-ftpext-hash-02

Some of the commands that can be used to calculate checksum are: XSHA1 , XSHA256 , XSHA512 , XMD5 , MD5 , XCRC and HASH .

You can test that with WinSCP FTP client . The WinSCP supports all the previously mentioned commands. Test its checksum calculation function or the checksum scripting command . If they work, enable logging and check, what command and what syntax WinSCP uses against your server. (I'm the author of WinSCP)

Once you find out, what command does your server support (if any), you can use FTP.voidcmd method :

ftp.voidcmd("XSHA1 " + filename)

I encountered a similar issue and came across the following solution, which would work regardless of whether server commands are possible

Source: https://smithje.github.io/python/2013/07/02/md5-over-ftp

In short, one could use the .retrbinary and load it into Hash object:

import ftplib
import hashlib

def get_ftp_md5(ftp, remote_path):
    m = hashlib.md5()
    ftp.retrbinary(f'RETR {remote_path}', m.update)
    return m.hexdigest()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM