python根据文件内容生成md5

  • Post author:
  • Post category:python


一、字符串生成MD5

import hashlib
import requests
def req(url):
    str = '1243456'
    return hashlib.md5(str.encode(encoding='utf-8')).hexdigest()

二、本地文件生成MD5

import hashlib
import requests
def req(url):
    with open('test.txt','rb') as f :
    	content = f.read()
    	return hashlib.md5(content).hexdigest()

三 、网络文件生成md5

import hashlib
import requests
def req(url):
    try:
        res = requests.get(url)
        return hashlib.md5(res.content).hexdigest()
    except:
        print("下载链接有问题:%s"%url)



版权声明:本文为weixin_44517891原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。