2013-01-12 65 views
0

我有一個python應用程序創建一個子進程,打開一個套接字與它通信,然後通過套接字創建一個multiprocessing.connection對象。連接對象使用共享密鑰(隨機生成)和hmac來確保不允許其他進程通過連接進行通信。python multiprocessing.connection報告「AuthenticationError:摘要收到錯了」

在Linux上,此功能完美無缺。在Windows中,我得到的錯誤:

multiprocessing.AuthenticationError: digest received was wrong 

最關鍵的是這是由它的標準輸入發送到子之前醃製隨機產生的比特串:

authkey = ''.join([chr(random.getrandbits(7)) for i in range(20)]) 

而且我仔細說檢查

print "key:", ' '.join([str(ord(x)) for x in authkey]) 

服務器開始:

在連接的兩端此-A-道路上的關鍵比賽210
l = multiprocessing.connection.Listener(
     ('localhost', int(port)), authkey=authkey) 

..和客戶端開始:

c = multiprocessing.connection.Client(
     ('localhost', int(port)), authkey=authkey) 

兩個進程都在同一臺機器上運行,用相同的Python版本。我發現如果我修復了關鍵字(比如authkey ='test'),那麼我第一次運行該程序時仍然會得到AuthenticationError,但是在後續運行時卻不會。

回答

0

該解決方案似乎是使用os.urandom來生成密鑰,而不是上面顯示的方法。我不知道爲什麼這會產生任何效果 - 無論哪種情況,我們都會傳遞隨機字節字符串。從安全角度來看,解決方案也更加正確,因爲getrandbits不適用於加密使用。

+0

嗨@Luke,你是怎麼用'os.urandom'生成密鑰的?我的服務器進程和客戶端進程分佈在兩臺不同的linux機器上。儘管他們使用相同的字符串鍵,但我得到了這個錯誤。 – Jon

+0

這只是'authkey = os.urandom(20)'。請參閱:https://github.com/pyqtgraph/pyqtgraph/blob/develop/pyqtgraph/multiprocess/processes.py#L73 – Luke