我創建了一個客戶端/服務器設定,用于將 PGP 簽名資訊從客戶端傳輸到服務器。下面的代碼顯示了服務器代碼的一部分,其中將從客戶端收到的簽名添加到輸出文本檔案中
但是,在中斷后,我的代碼在第二個 while 回圈之后無法繼續。收到客戶端的2個簽名,只成功列印了兩次“test”字串,并將接收到的兩個字串都添加到輸出檔案中,但程式中斷后不會繼續,也不會列印其他的“test2”和“test3” "字串。
while True:
# Accepts incoming connection, creating socket used for data transfer to client
conn, addr = server.accept()
print("Connected successfully")
directory = "(Hidden for question)"
outputFile = open((directory "\\signatures.txt"), "w")
while True:
data = conn.recv(2048)
if not data: break
print("test")
outputFile.write(data.decode() "\n")
outputFile.flush()
print("test2")
conn.close()
print("test3")
我覺得我遺漏了一些非常明顯的東西,但無法弄清楚問題是什么。
uj5u.com熱心網友回復:
您的回圈永遠不會中斷,因為recv套接字上的函式是阻塞呼叫。這意味著該函式在接收到一些資料之前不會回傳,因為not data它將始終為假。
嘗試將更多資訊(在前 2 個簽名之后)發送到套接字中,并查看您的腳本是否會繼續將其寫入檔案。
如果您想接收特定數量的資料/時間,請使用變數跟蹤它并使用該變數中斷回圈。
uj5u.com熱心網友回復:
或者@Nadav 的答案,洗掉內while回圈。由于recv()是同步的,您不需要回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371391.html
