Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/d96ffb86311e3a5569067b5beb0f7aa1 to your computer and use it in GitHub Desktop.
Save dacr/d96ffb86311e3a5569067b5beb0f7aa1 to your computer and use it in GitHub Desktop.
just check if a network tcp port is accessible / published by https://github.com/dacr/code-examples-manager #464f832c-949c-403b-b302-9045d7ca3f91/6a502874742c653e06c41982ae03d11cca6d911f
#!/usr/bin/env python3
## summary : just check if a network tcp port is accessible
## keywords : python, test, network
## publish : gist
## authors : David Crosson
## license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
## id : 464f832c-949c-403b-b302-9045d7ca3f91
## created-on : 2020-10-23T11:46:13Z
## managed-by : https://github.com/dacr/code-examples-manager
## run-with : python3 scriptname.sc
import socket
import sys
def check(host,port,kind):
try:
sock = socket.socket(kind, socket.SOCK_STREAM)
target = (host, port)
sock.connect(target)
return True
except Exception as e:
return False
def check4(host,port):
return check(host, port, socket.AF_INET)
def check6(host,port):
return check(host, port, socket.AF_INET6)
host = sys.argv[1] if len(sys.argv) > 1 else 'mapland.fr'
port = int(sys.argv[2] if len(sys.argv) > 2 else '80')
print("checkIPV4="+("OK" if check4(host, port) else "KO"))
print("checkIPV6="+("OK" if check6(host, port) else "KO"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment