Python Forum
testing for a network connection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
testing for a network connection
#6
Don't try to solve it with checking everything before.
Work with timeouts. Set the timeout to your socket, then it will throw a socket.timeout exception if you try to read a file and the read took longer than the timeout.

import socket


def read(fd):
    try:
        fd.read(1)
    except socket.timeout:
        print("Read timed out")


s = socket.socket()
s.settimeout(10) # 10 seconds
s.connect(("google.com", 80))
f = s.makefile()

# will fail
read(f)
Regular files won't throw a socket.timeout.
I hope this is a better answer.

By the way, how do you check if a socket is still connected?
I think there is no safe method.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
testing for a network connection - by Skaperen - Jun-16-2020, 01:02 AM
RE: testing for a network connection - by DeaD_EyE - Jun-16-2020, 05:12 PM
RE: testing for a network connection - by Skaperen - Jun-16-2020, 10:34 PM
RE: testing for a network connection - by DeaD_EyE - Jun-17-2020, 09:32 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020