2012-04-18 58 views
1

我正在嘗試使用Python 3計算來自stdin的文件的md5哈希值Python 3如何從stdin上的文件生成md5哈希?

這是返回的錯誤消息。我看不出爲什麼它不返回md5散列。任何幫助讚賞。

$./pymd5.py < tmp.pdf 
Traceback (most recent call last): 
    File "./pymd5.py", line 29, in <module> 
    main() 
    File "./pymd5.py", line 25, in main 
    print(m.hexdigest()) 
TypeError: 'str' does not support the buffer interface 
$ 

代碼:當你做

sys.stdout = sys.stdout.detach() 

#!/usr/local/bin/python3.2 

import sys 
import hashlib 

BUFSIZE = 4096 

def make_streams_binary(): 
    sys.stdin = sys.stdin.detach() 
    sys.stdout = sys.stdout.detach() 

def main(): 
    make_streams_binary() 
    m = hashlib.md5() 
    while True: 
     data = sys.stdin.read(BUFSIZE) 
     if not data: 
      break 
     m.update(data) 

    print(m.hexdigest()) 

if __name__ == "__main__": 
    main() 
+0

可能重複的[TypeError:'str'不支持緩衝區接口](http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface) – agf 2012-04-18 04:12:23

+0

I對不起,這並不能真正解釋問題所在。 – 2012-04-18 04:13:18

回答