Created
May 30, 2018 04:42
-
-
Save blitzblade/0f3ff9360590b3f28e371198fdde7c08 to your computer and use it in GitHub Desktop.
This is a piece of code for checking if an ip:port is open and also do continuous checks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from time import sleep | |
def is_open(ip,port,timeout=5): | |
socket.setdefaulttimeout(timeout) | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
s.connect((ip, int(port))) | |
s.shutdown(2) | |
return True | |
except: | |
return False | |
def keep_checking(ip,port,intervals=5,rounds=10,infinite=False): | |
def check(): | |
if is_open(ip, port): | |
print("{}:{} is open...".format(ip, port)) | |
else: | |
print("{}:{} is closed...".format(ip, port)) | |
print("sleeping for {} second(s)".format(intervals)) | |
sleep(intervals) | |
if infinite: | |
while True: | |
check() | |
else: | |
for i in range(rounds): | |
check() | |
if __name__=='__main__': | |
keep_checking('127.0.0.1',5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment