Python比较本地和远程文件MD5哈希

Python比较本地和远程文件MD5哈希

本文介绍了Python比较本地和远程文件MD5哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较本地和远程文件MD5哈希(我复制/粘贴在wamp"www"目录中的同一文件),但是我不明白为什么校验和"不对应...

I am trying to compare Local and remote file MD5 hash (the same file i copy/paste in my wamp "www" directory), but I don't understand why the "checksums" are not corresponding...

这是校验和代码:

#-*- coding: utf-8 -*-

import hashlib
import requests

def md5Checksum(filePath,url):
    if url==None:
        with open(filePath, 'rb') as fh:
            m = hashlib.md5()
            while True:
                data = fh.read(8192)
                if not data:
                    break
                m.update(data)
            return m.hexdigest()
    else:
        r = requests.get(url, stream=True)
        m = hashlib.md5()
        for line in r.iter_lines():
            m.update(line)
        return m.hexdigest()

print "checksum_local :",md5Checksum("projectg715gb.pak",None)
print "checksum_remote :",md5Checksum(None,"http://testpangya.ddns.net/projectg715gb.pak")

我很惊讶获得此输出:

checksum_local : 9d33806fdebcb91c3d7bfee7cfbe4ad7
checksum_remote : a13aaeb99eb020a0bc8247685c274e7d

"projectg715gb.pak"的大小为14.7Mb

The size of "projectg715gb.pak" is 14.7Mb

但是,如果我尝试使用文本文件(大小为1Kb):

But if I try with a text file (size 1Kb) :

print "checksum_local :",md5Checksum("toto.txt",None)
print "checksum_remote :",md5Checksum(None,"http://testpangya.ddns.net/toto.txt")

然后它起作用,我得到以下输出:

Then it works oO I get this output :

checksum_local : f71dbe52628a3f83a77ab494817525c6
checksum_remote : f71dbe52628a3f83a77ab494817525c6

我是比较MD5哈希的新手,所以请好^^'我可能犯了一些大错误,我不明白为什么它不适用于大文件,如果有人可以给我一个提示,它将超级好!

I am new to comparing MD5 hash so be nice please ^^' I might have done some big mistake, I don't understand why it doesn't work on big files, if someone could give me a hint, it would be super nice!

不过,感谢您的阅读和帮助!

However thanks for reading and helping !

推荐答案

好吧,我好像找到了解决方案,所以我将其发布在这里:)

Ok looks like i found a solution so i will post it here :)

首先,您需要将 .htaccess 文件编辑到文件在服务器上的目录中.

First you need to edit an .htaccess file to the directory where your files are on your server.

.htaccess 文件的内容:

ContentDigest On

现在您已经设置好了,服务器应该在HTTP标头中发送 Content-MD5 数据.

Now that you have set up this the server should send Content-MD5 data in HTTP header.

这将导致类似:

'Content-MD5': '7dVTxeHRktvI0Wh/7/4ZOQ=='

现在让我们看一下Python部分,所以我修改了代码以能够比较此HTTP标头数据和本地md5校验和.

Ok now let see Python part, so i modified my code to be able to compare this HTTP header data and local md5 Checksum.

#-*- coding: utf-8 -*-

import hashlib
import requests
import base64

def md5Checksum(filePath,url):
    m = hashlib.md5()
    if url==None:
        with open(filePath, u'rb') as fh:
            m = hashlib.md5()
            while True:
                data = fh.read(8192)
                if not data:
                    break
                m.update(data)
            #Get BASE 64 Local File md5
            return base64.b64encode(m.digest()).decode('ascii')#Encode MD5 digest to BASE 64

    else:
        #Get BASE 64 Remote File md5
        r = requests.head(url) #You read HTTP Header here
        return r.headers['Content-MD5'] #Take only Content-MD5 string

def compare():
    local = md5Checksum("projectg502th.pak.zip",None)
    remote = md5Checksum(None,"http://127.0.0.1/md5/projectg502th.pak.zip")

    if local == remote :
        print("The soft don't download the file")
    else:
        print("The soft download the file")

print ("checksum_local :",md5Checksum("projectg_ziinf.pak.zip",None))
print ("checksum_remote : ",md5Checksum(None,"http://127.0.0.1/md5/projectg_ziinf.pak.zip"))

compare()

输出:

checksum_local : 7dVTxeHRktvI0Wh/7/4ZOQ==
checksum_remote : 7dVTxeHRktvI0Wh/7/4ZOQ==
The soft don't download the file

我希望这会有所帮助;)

I hope this will help ;)

这篇关于Python比较本地和远程文件MD5哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:22