How to keep trying to establish connection in Python
Question: If the server is not up when I try to run the following code, I just get a
Connection refused error.

How can I make the sender below to keep trying to establish connection and
perhaps sending until the remote server is indeed up and the connection is
successfully established?

    
    
    HOST = client_ip    # The remote host
        PORT = port        
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
        s.connect((HOST, PORT))
        s.sendall(msg)
    
        if expect_receive:
            received_data = s.recv(1024)
            print received_data
            #client has started
    
        s.close()
        return
    


Answer: How about brute force? Something like this

    
    
    import time
    while 1:
        HOST = client_ip    # The remote host
        PORT = port       
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
        try:
            s.connect((HOST, PORT))
        except:
            print("FAILED. Sleep briefly & try again")
            time.sleep(10)
            continue
        s.sendall(msg)
    
        if expect_receive:
            received_data = s.recv(1024)
            print received_data
            #client has started
    
        s.close()
        return
    

