[英]Python Threading Unhandled Exception
問題
異常的原因是什么?
客戶端是否導致任何錯誤?
如果可能的話,請解釋其他錯誤。
背景
我正在創建一個Python GUI套接字服務器。 當客戶端連接到我的服務器時,GUI窗口將打開(我仍在處理此問題)。 但是,當客戶端連接時,我收到一個錯誤:
Unhandled exception in thread started by <function clientthread at 0x10246c230>
由於實際腳本相當長,我提供了一個pastebin鏈接。
這是線程代碼。 s
是我的socket
對象的名稱。
def clientthread(s):
#Sending message to connected client
#This only takes strings (words
s.send("Welcome to the server. Type something and hit enter\n")
#loop so that function does not terminate and the thread does not end
while True:
#Receiving from client
data = s.recv(1024)
if not data:
break
s.sendall(data)
print data
s.close()
追溯
感謝Morten的建議。 這是追溯。
Socket Created
Socket Bind Complete
Socket now listening
Connected
Traceback (most recent call last):
File "/Users/BigKids/Desktop/Coding/Python 2/Sockets/Function/Server GUI Alpha Function.py", line 80, in clientthread
s.send("Welcome to the server. Type something and hit enter\n")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 170, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor
就個人而言,我認為很多錯誤都是由GUI造成的。
謝謝!
首先,您可以捕獲異常,打印它,看看它是什么:)
這樣做,例如通過try / except子句包圍它並打印出現的任何異常。
def clientthread(s):
try:
#Sending message to connected client
#This only takes strings (words
s.send("Welcome to the server. Type something and hit enter\n")
#loop so that function does not terminate and the thread does not end
while True:
#Receiving from client
data = s.recv(1024)
if not data:
break
s.sendall(data)
print data
s.close()
except Exception:
import traceback
print traceback.format_exc()
我猜這是客戶端斷開的原因。 這將導致異常,您應該適當地處理它。 如果客戶端可以通過多種方式斷開連接。 告訴你,通過超時,在你嘗試發送東西時刪除連接等等。所有這些情況都是合理的異常情況,你應該測試它們並處理它們。 希望這會幫助你繼續前進,如果沒有,請評論:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.