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()
Use this Socket code to PIng the server. To extract host and port from the URL use regex group.