def random_useragent(filename=True):
    import random
    default_ua = 'urlfetch/%s' % __version__
    if isinstance(filename, basestring):
        filenames = [filename]
    else:
        filenames = []
    if filename and UAFILE:
        filenames.append(UAFILE)
    for filename in filenames:
        try:
            st = os.stat(filename)
            if stat.S_ISREG(st.st_mode) and os.access(filename, os.R_OK):
                break
        except:
            pass
    else:
        return default_ua
    with open(filename, 'rb') as f:
        filesize = st.st_size
        pos = 0
        r = random.Random()
        for i in range(3):
            pos += r.randint(0, filesize)
            pos %= filesize
            f.seek(pos)
            f.readline()
            line = f.readline()
            if not line:
                if f.tell() == filesize:
                    f.seek(0)
                    line = f.readline()
            line = line.strip()
            if line and line[0] != '
                return line
    return default_ua