From 64500de593587f8e2853121ec854da12443429be Mon Sep 17 00:00:00 2001 From: Clayton Errington Date: Tue, 12 May 2020 15:19:43 -0500 Subject: [PATCH] 0.0.7 --- netutil/__init__.py | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 netutil/__init__.py diff --git a/netutil/__init__.py b/netutil/__init__.py new file mode 100644 index 0000000..2f34cc7 --- /dev/null +++ b/netutil/__init__.py @@ -0,0 +1,72 @@ +import socket, time + + +__all__ = ['netutil'] +__version__ = "0.0.7" +__author__ = "Clayton Errington" + + +class PortCheck: + def __init__(self, domain, port): + self.domain = domain + self.port = port + self.retry = 1 + self.delay = 5 + self.timeout = 3 + + + def __call__(self, domain, port): + self.domain = domain + self.port = port + + + def checkOpen(self): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(self.timeout) + try: + s.connect((self.domain, int(self.port))) + s.shutdown(socket.SHUT_RDWR) + s.close() + return True + except: + return False + finally: + s.close() + + + def checkHost(self): + ipup = False + for i in range(self.retry): + if self.checkOpen(): + ipup = True + break + else: + time.sleep(self.delay) + return ipup + + + def isOpen(self): + if self.checkHost(): + # If connects, then we are true + return True + else: + # If connection fails, we are false + return False + + + def domain(self): + # Allow for domain to be returned + return self.domain + + + def port(self): + # Allow for port to be returned + return self.port + + +# return variables for use +host_name = socket.gethostname() +host_ip = socket.gethostbyname(host_name) +isOpen = PortCheck.isOpen +domain = PortCheck.domain +port = PortCheck.port