The connection of python to some ip

Campus network needs to be authenticated. If not,

s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.settimeout(3)
s.connect(("ip",80))
s.close()

also works properly.
ask how to test whether the network of the specified ip is working.

Jul.06,2021

s.connect (('some ip',80)) if there is an error in the connection, it will return the error of socket.error. Can you judge by this error?


something like this

try:
    if protocol.upper() == 'UDP':
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    else:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    if port == 443:
        sock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_SSLv23)

    sock.settimeout(2)
    sock.connect((address, port))
    return True
except socket.error as e:
    return False
except Exception as e:
    return False
finally:
    sock.close()

Menu