def get_random_bytes(num_bytes, egd_path=None, blocking=True):
    """Gets random bytes from an EGD daemon.

    Attempts to return a string of num_bytes length with each character
    having a random byte value.  Requests for more than 255 bytes may
    result in multiple low-level requests to the EGD daemon.

    You may specify either a pathname to the EGD daemon's Unix domain
    socket, or a TCP port number, or a TCP address in the format
    "hostname:portnumber".  If you do not supply a path, a set of
    commonly used paths will be tried.

    If blocking is true, then the EGD daemon is queried in blocking mode,
    meaning that this function may be delayed in returning depending on
    how much entropy is in the EGD pool.  If blocking is false, then there
    is a chance you may not receive as much random data as requested,
    but you should not be indefinitely delayed.

    """
    if num_bytes < 1:
        return ''
    import socket
    if not egd_path:
        import os, stat
        for egd_path in ['/dev/egd-pool', '/var/run/egd-pool', '/etc/egd-pool'
            ]:
            try:
                if os.stat(egd_path)[stat.ST_MODE] & stat.S_IFSOCK:
                    break
            except OSError:
                pass
        else:
            raise RuntimeError('No EGD socket found')
        addr = egd_path
        addr_family = socket.AF_UNIX
    else:
        try:
            portnum = int(egd_path)
        except ValueError:
            if ':' in egd_path and '/' not in egd_path:
                addr_family = socket.AF_INET
                addr = egd_path.split(':', 1)
                try:
                    addr[1] = int(addr[1])
                except ValueError:
                    addr[1] = socket.getservbyname(addr[1], 'tcp')
                addr = tuple(addr)
            else:
                addr_family = socket.AF_UNIX
                addr = egd_path
        else:
            addr_family = socket.AF_INET
            addr = 'localhost', portnum
    output = ''
    sok = socket.socket(addr_family, socket.SOCK_STREAM)
    try:
        sok.connect(addr)
        while len(output) < num_bytes:
            if blocking:
                cmd = '\x02'
            else:
                cmd = '\x01'
            cmd += chr(min(255, num_bytes - len(output)))
            sok.send(cmd)
            received = sok.recv(256)
            if len(received) == 0:
                if not blocking:
                    break
            elif blocking:
                output += received
            else:
                cnt = ord(received[0])
                output += received[1:1 + min(cnt, len(received) - 1)]
    finally:
        sok.close()
    return output
