Questions
What is the reason for the exception?
Did the client cause any errors?
If at all possible, please explain other errors.
Background
I am creating a Python GUI socket Server. When a client connects to my server, the GUI window will open (I am still working on this). But, when a client does connect, I get an error:
Unhandled exception in thread started by <function clientthread at 0x10246c230>
Since the actual script is rather long, I have provided a pastebin link.
Here is the thread 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 Morten. Here is the traceback.
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 due to the GUI.
Thanks!
For one, you could catch the exception, print it and see what it is :)
Do this, for instance by surrounding it all with a try/except clause and printing whatever exception occurs.
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'm guessing the reason for this is client disconnect. This will cause an exception and you should handle it appropriately. If a client can disconnect in many ways. By telling you, by timing out, by dropping the connection while you're trying to send something etc. All these scenarios are plausible exception cases, and you should test for them and handle them. Hopefully this will help you move on, if not, please comment :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With