1

Hi I'm playing around with the sockets in Python 3.6 and I want to know if my way of checking whether a certain socket is closed or not in the server side is right. Here is the code:

try: 
    data = s.recv(1024)
    print(data)
except socket.error as msg:
    print("disconnected")
    s.close()

I had tried the one that checks if data variable is empty but it kept giving me errors when the socket is closed. That's why I had used try catch. Am I doing it right?

DongSeong Seo
  • 43
  • 1
  • 5
  • Possible duplicate of [How to tell if a connection is dead in python](https://stackoverflow.com/questions/667640/how-to-tell-if-a-connection-is-dead-in-python) – ndmeiri Feb 19 '18 at 00:41
  • yes i had read that and it says "For python, you'll read a zero length string" but when I read the string that is received to check if it is empty, it gives error so IDK which is correct. – DongSeong Seo Feb 19 '18 at 01:10
  • Please read the answers in that question more carefully. There are multiple detailed and thoughtful answers besides the accepted one. – ndmeiri Feb 19 '18 at 01:59

0 Answers0