import socket def ping(): HOST = "127.0.0.1" PORT = 9999 payload = b"ping\0" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) while True: try: s.sendall(payload) print("> ping") except Exception as e: break reply = s.recv(1024).decode() if reply == "": break print(f"< {reply}") print("connection shut down") if __name__ == "__main__": ping()