In python you can do a try-except statement like this:
try:
conn.send("{you can send anything to check connection}")
except BrokenPipeError:
print("Client has Disconnected")
This works because when the client/server closes the program, python returns broken pip error to the server or client depending on who it was that disconnected.