Hey all,

I've been experimenting with a basic chat server/client from my Python textbook, in efforts to keep the code clean and simple I think some optional code was omitted and it's made the books example a little buggy.

I've been having troubles with the line:

transmission = self.request.recv(1024)

This is server side, awaiting information from a client.

The problem I'm having is if a client connects, sends chat for a while and then disconnects in a non expected way (terminate the process, stop the execution of code, internet cuts out, etc) the server crashes.

So I believe this should be wrapped in a try

    transmission = self.request.recv(1024)
except: #should this be a specific exception only, or a general one
    #What should I put here?

Any help on that last code snippet would be appreciated. Thanks!

6 Years
Discussion Span
Last Post by Gribouillis

Since the server crashed, it displayed the exception, probably a socket.error, so use except socket.error . In except block, put the code that you want to execute when the client disconnects in an unexpected way (it could be a simple pass statement). Also, if these lines are in a loop which reads from the client, you must exit that loop once you know that the error happened.

Edited by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.