Created
March 30, 2018 11:17
-
-
Save nooperpudd/5eeec10761608e7a6c9198609c252c9a to your computer and use it in GitHub Desktop.
check address port is down or not
This file contains hidden or 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 | |
import sys | |
import urllib.parse | |
from contextlib import closing | |
def check_address_port(tcp): | |
""" | |
:param tcp: | |
:return: | |
""" | |
host_schema = urllib.parse.urlparse(tcp) | |
ip = host_schema.hostname | |
port = host_schema.port | |
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: | |
if sock.connect_ex((ip, port)) == 0: | |
return True # OPEN | |
else: | |
return False # closed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment