[root@aws105-24srv01 apps]# cat custom-python/aw1041-BatchCountHash.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "月影鹏鹏"
__date__ ="$2011-2-16 17:39:40$"
'''
QQ:1356281767
email: jk@aiwaly.com
web: http://www.aiwaly.com
'''
import os,time ,sys ,shutil ,hashlib ,json, string
#获得文件后缀
def file_extension(path):
return os.path.splitext(path)[1]
def CalcSha1(filepath):
with open(filepath,'rb') as f:
sha1obj = hashlib.sha1()
sha1obj.update(f.read())
hash = sha1obj.hexdigest()
#print(hash)
f.close()
return hash
def CalcMD5(filepath):
with open(filepath,'rb') as f:
md5obj = hashlib.md5()
md5obj.update(f.read())
hash = md5obj.hexdigest()
#print(hash)
f.close()
return hash
class HashInfoJson(object):
def __init__(self, uuid,filename, sha1check,md5check,fsize ,extname):
self.filename = filename
self.sha1check = sha1check
self.md5check = md5check
self.fsize = fsize
self.extname = extname
self.uuid = uuid
def GetFileHashInfo(filepath):
print(filepath)
fname,ext = os.path.splitext(filepath)
FileInfo_extname =ext
FileInfo_filename =os.path.basename(fname) + ext
FileInfo_sha1check=CalcSha1(filepath)
FileInfo_md5check=CalcMD5(filepath)
FileInfo_fsize = os.path.getsize(filepath)
FileInfo = {"keyid":FileInfo_sha1check , "filename": FileInfo_filename , "sha1check": FileInfo_sha1check ,"md5check": FileInfo_md5check ,"fsize": FileInfo_fsize ,"extname":FileInfo_extname.lower() }
print(json.dumps(FileInfo ,ensure_ascii=False) )
try:
FileHashInfoPost(FileInfo)
except:
print("commit error")
#FileInfo = HashInfoJson( "ddddd" , FileInfo_filename , FileInfo_sha1check ,FileInfo_md5check ,FileInfo_fsize , FileInfo_extname )
#auth.aiwaly.com 登录验证获得token
def FileHashInfoPost(FileInfo):
import json
import urllib.parse
import urllib.request
url = "http://10.26.105.24:11037/posthash"
#data = {"username": str(username),"password": str(password) }
data = json.dumps(FileInfo ,ensure_ascii=False)
data=bytes(data,'utf8')
#print(data )
#data1 = urllib.parse.quote_plus(data )
#data1 = urllib.parse.quote_plus(data).encode(encoding='UTF8')
#print(data1 )
req = urllib.request.Request(url, data)
req.add_header('Content-Type','application/json' )
#print(type(request))
result=urllib.request.urlopen(req).read()
json_str = json.loads( result )
print(result)
#return json_str['code'] , json_str['data']
#code_status , token = login_auth("admin","admin" )
#print(code_status , token )
def file_path(path):
for fpathe,dirs,fs in os.walk(path):
for f in fs :
print( os.path.join(fpathe,f))
extension_name =file_extension (os.path.join(fpathe,f))
if extension_name.lower() == ".jpg" or extension_name == ".CR2" or extension_name == ".DNG":
GetFileHashInfo(os.path.join(fpathe,f))
#if extension_name == ".CR2" or extension_name == ".DNG" :
# FilenameAddSHA1andGzip(os.path.join(fpathe,f))
if __name__ == '__main__':
path="/awdisk0010/nas/AWDATA/myphoto/Rp2017/"
print(path)
file_path(path)
[root@aws105-24srv01 apps]#
文档更新时间: 2019-06-14 13:38 作者:月影鹏鹏