Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:18
Show Gist options
  • Select an option

  • Save dacr/d96ffb86311e3a5569067b5beb0f7aa1 to your computer and use it in GitHub Desktop.

Select an option

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/2cc0bc486251381cfe9b63e430f3a7c0a989503b
#!/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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
## 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