Unspecified Python Threading Exception

Questions

  • What is the reason for the exception?

  • Did the client cause any errors ?

  • If at all possible, explain the other errors.

Background

I am creating a Python GUI socket server. When a client connects to my server, a GUI window will open (I'm still working on this). But when the client connects, I get an error message:

Unhandled exception in thread started by <function clientthread at 0x10246c230> 

Since the actual script is quite long, I provided a pastebin link.

Here is the stream code. s is the name of my socket object.

 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() 

Traceback

Thanks for the suggestion of Morten . Here is the trace.

 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 

Personally, I believe that many errors are related to the graphical interface.

Thanks!

+7
source share
1 answer

First, you can catch the exception, print it out and see what it is :)

Do this, for example, by surrounding it all with a try / except clause and printing any exception.

 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() 

I assume the reason for this is to disconnect the client. This will throw an exception, and you should handle it accordingly. If the client can disconnect in many ways. By telling you, in time, disconnecting the connection while you are trying to send something, etc. All of these scenarios are plausible exceptions, and you should check them out and handle them. Hope this helps you move on, if not, please comment :)

+2
source

All Articles