【问题标题】:Check if MD5 value exists in an index file检查索引文件中是否存在 MD5 值
【发布时间】:2018-11-26 19:37:41
【问题描述】:

我正在尝试找出一种方法来验证我的代码是否可以交叉验证索引文件中是否存在 url 字符串的 md5 转换值,如果可以则跳过扫描。

下面是我的代码

形成的 url 转换为 md5 字符串,然后在扫描完成后存储在 idx 文件中,目标是以后的扫描不应拾取相同的 url。我看到的问题是 if str(md5url) in line 没有被执行,可能是因为在将哈希添加到文件时没有使用 '\n' 作为后缀。但我试过了,还是不行。

有什么想法吗?

def computeMD5hash(string_for_hash):
    m = hashlib.md5()
    m.update(string_for_hash.encode('utf-8'))
    return m.hexdigest()


def writefilehash(formation_URL):
    fn="urlindex.idx"
    try:
        afile = open(fn, 'a')
        afile.write(computeMD5hash(formation_URL))
        afile.close()
    except IOError:
        print("Error writing to the index file")

fn="urlindex.idx"
try:
    afile = open(fn, 'r')
except IOError:
    afile = open(fn, 'w')

for f in files:
    formation=repouri + "/" + f
    #print(computeMD5hash(formation))
    md5url=computeMD5hash(formation)
    hashlist = afile.readlines()
    for line in hashlist:
        if str(md5url) in line:
            print ("Skipping " + formation + " because its already scanned and indexed as  " + line)
        else:
            if downloadengine(formation):
                print ("Download completed " + formation)
                print ("Starting to write to database..")
                #writetodatabase()
                print ("Writing hash value ..")
                writefilehash(formation)

print("Closing..")
afile.close()

【问题讨论】:

  • 您正在将一长行中的所有哈希值写入文件中。 md5url in line 检查仍然会成功,但是换行在那里不起作用,in 测试 containment,而不是相等性。

标签: python md5 scanning


【解决方案1】:

您正在循环测试。对于不匹配的每一行,您下载:

line1
    if hash in line:
        print something
    else
        download
line2
    if hash in line:
        print something
    else
        download
line3
    if hash in line:
        print something
    else
        download

如果哈希在第 1 行,那么您仍然下载,因为哈希不在第 2 行或第 3 行。您不应该决定下载直到您测试所有行

执行此操作的最佳方法是一次性将所有哈希读取到集合对象中(因为针对集合进行包含测试更快)。删除行分隔符:

try:
    with open(fn) as hashfile:
        hashes = {line.strip() for line in hashfile}
except IOError:
    # no file yet, just use an empty set
    hashes = set()

然后在测试新哈希时使用:

urlhash = computeMD5hash(formation)
if urlhash not in hashes:
    # not seen before, download
    # record the hash
    hashes.add(urlhash)
    with open(fn, 'a') as hashfile:
        hashfile.write(urlhash + '\n')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2019-10-12
    • 2014-01-26
    相关资源
    最近更新 更多