import socket


def udp_connection_test(ipaddr: str, port: int, timeout=1, message=b"PING"):
    """tests udp connection on specified port by sending specified message

    Args:
        ipaddr (str): ip address
        port (int): port number
        timeout (int, optional): time before the connection test fails. Defaults to None.
        message (str, optional): message to send. Defaults to b"PING"

    Returns:
        bool: True if the test succeeds, False otherwise
    """
    conn = socket.socket(type=socket.SOCK_DGRAM)
    if timeout:
        conn.settimeout(timeout)
    try:
        conn.connect((ipaddr, port))
    except BaseException:
        conn.close()
        return False

    conn.send(message)

    try:
        conn.recvfrom(8192)
    except Exception:
        return False
    return True