unipolar-soft / substation_logger

A logger aka Historian for substation
0 stars 0 forks source link

Check API server status from API settings windows #9

Open kBashar opened 1 year ago

kBashar commented 1 year ago

Use this Socket code to PIng the server. To extract host and port from the URL use regex group.

import socket

# define the server address and port
server_address = ('example.com', 80)

# create a new socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# set a timeout of 5 seconds
sock.settimeout(5)

# try to connect to the server
try:
    sock.connect(server_address)
    print(f"Server {server_address[0]}:{server_address[1]} is reachable.")
except socket.error as e:
    print(f"Unable to connect to server {server_address[0]}:{server_address[1]}. Error: {e}")

# close the socket
sock.close()