简体   繁体   中英

I got a problem with subprocess.call() python

Good Day,

I made a script that help me to comparing md5 hash of apps if it's official or not. As you know when you go to App official website you will see an official md5 hash for app to compare it with other copies.

My problem is when I test my script always gave me NOT OFFICIAL even if md5 Hash are same.

Here is my code

import subprocess

Hash = str.strip(input())
print(Hash)

Dir = str.strip(input())
print(Dir)

output = subprocess.check_output("md5 " + Dir, shell=True)
print(output)

if Hash == output:
    print("OFFICIAL")

else:
    print("NOT OFFICIAL")

and the output is:

b'MD5 (/Users/username/Desktop/test.py) =
86ece1d78b65fdb40058a3514d90df9d\n' NOT OFFICIAL

Is there any problem with my code?

Thanks..

You need to decode() to convert output from bytes to str . Further, you also need to strip() it since otherwise you will be comparing a string with a newline vs. one without it. Finally, you may need to parse the output to remove whatever the md5 utility prints alongside the hash (if anything).


Note: instead of spawning a process, a better solution is to use the standard hashlib module, which will also make your script portable, simpler and faster.

Note 2: you should use other newer hashes if possible.

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