diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6136fa7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build/ +dist/ +netutils.egg-info \ No newline at end of file diff --git a/README.md b/README.md index 35be2ef..cf28816 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ # netutils -python module to check the status of ports for local and websites. +Python module to check the status of ports for local and websites. + +# Install +pip install netutils + +# Usage +```python +import netutils +google = netutils.PortCheck("google.com", 443) +google.isOpen() +``` + +For extra help I've added in variables to find the hostname and local IP address. +- To get hostname: netutils.host_name +- To get Local IP: netutils.host_ip \ No newline at end of file diff --git a/netutils/__init__.py b/netutils/__init__.py index 8b13789..e14f7a9 100644 --- a/netutils/__init__.py +++ b/netutils/__init__.py @@ -1 +1,72 @@ +import socket, time + +__all__ = ['netutils'] +__version__ = "0.0.6" +__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 \ No newline at end of file diff --git a/netutils/netutils.py b/netutils/netutils.py deleted file mode 100644 index 8a7e70b..0000000 --- a/netutils/netutils.py +++ /dev/null @@ -1,58 +0,0 @@ -''' - Class to check if port connection is open or not. - - Author: Clayton Errington -''' -import socket, time - -host_name = socket.gethostname() -host_ip = socket.gethostbyname(host_name) - -class PortCheck: - def __init__(self, domain, port): - self.domain = domain - self.port = port - self.retry = 1 - self.delay = 5 - self.timeout = 3 - - - 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(): - #print(f"{self.domain}:{self.port} is {Fore.GREEN}OPEN {Fore.RESET}") - return True - else: - #print(f"{Fore.RED}{self.domain}:{self.port} is CLOSED {Fore.RESET}") - return False - - -if __name__ == "__main__": - test = PortCheck("google.com", 443) - test2 = PortCheck("google.com", 25).isOpen() - print(test.isOpen()) - print(test2) diff --git a/setup.py b/setup.py index d37c702..523a18a 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="netutils", - version="0.0.4", + version="0.0.6", author="Clayton Errington", author_email="me@claytonerrington.com", description="Simplified way to get networking port status",