Skip to content

Instantly share code, notes, and snippets.

@catichenor
Forked from betrcode/README.md
Last active October 28, 2019 22:48
Show Gist options
  • Save catichenor/3adafe8123abf91b91c01fd2e925f46b to your computer and use it in GitHub Desktop.
Save catichenor/3adafe8123abf91b91c01fd2e925f46b to your computer and use it in GitHub Desktop.
Using Python to check if remote port is open and accessible.
import time
import socket
ip = "mylicenseserver.mydomain.com"
port = 27000
responses = []
for i in range(10):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
s.connect((ip, port))
responses.append("{} is UP".format(ip))
except:
time.sleep(1)
finally:
try:
s.shutdown(socket.SHUT_RDWR)
s.close()
except OSError:
responses.append("{} is DOWN".format(ip))
results = "\n".join(responses)
if "DOWN" in results:
print("Server is down.")
else:
print("Server is up!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment