Dup Ver Goto 📝

WaitCon

PT2/lang/python/net does not exist
To
37 lines, 84 words, 776 chars Page 'WaitCon' does not exist.

Simple script to wait for a connection to host/port. (Waiting for a ping means that a ping may succeed when a host is not ready for ssh or http.)

#!/usr/bin/env python3

import socket
import sys
import time
from datetime import datetime

try:
  host,port = sys.argv[1:]
  port = int(port)
except Exception:
  print(f"{sys.argv[0]} <host> <port>")
  exit(1)

def printnow():
  now = datetime.now()
  print(now.strftime("%c"))

try:
  while True:
    with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
      s.settimeout(1)
      try:
        s.connect((host,port))
        s.close()
        exit(0)
      except (OSError,TimeoutError,ConnectionRefusedError) as e:
        printnow()
        time.sleep(1)
except KeyboardInterrupt:
  print("Ctrl-c")
  exit(0)