Python线程未处理异常
问题Python线程未处理异常,python,multithreading,sockets,Python,Multithreading,Sockets,问题 例外的原因是什么 有没有任何错误的原因 如果可能,请解释其他错误 背景 我正在创建一个Python GUI套接字服务器。当客户端连接到我的服务器时,GUI窗口将打开(我仍在处理此问题)。但是,当客户端连接时,我会得到一个错误: Unhandled exception in thread started by <function clientthread at 0x10246c230> 回溯 谢谢你的建议。这是回溯 Socket Created Socket Bind Comp
Unhandled exception in thread started by <function clientthread at 0x10246c230>
回溯
谢谢你的建议。这是回溯
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()
我猜这是因为客户端断开连接。这将导致异常,您应该适当地处理它。如果客户端可以通过多种方式断开连接。通过告诉你,通过超时,通过在你试图发送东西时中断连接等等。
所有这些场景都是合理的异常情况,您应该测试并处理它们。希望这将帮助您继续前进,如果没有,请评论:)您的代码对我来说很好。是什么导致了异常?你不认为可能是客户端断开了吗?这通常会引发一个例外IIRC@MortenJensen我添加了我的客户。我会在15分钟后回来检查-我必须去和erg:)@xxmbanexx坏文件描述符意味着你试图写入的套接字是“坏的”。这可能意味着客户端断开连接。异常转储中的's'是什么?客户端套接字?
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()