def limited_transfer_data(from_function, to_function, max_bytes):
    """transfers up to max_bytes from from_function to to_function
    or as many bytes as from_function generates as strings"""
    BUFFER_SIZE = 1048576
    s = from_function(BUFFER_SIZE)
    while len(s) > 0 and max_bytes > 0:
        if len(s) > max_bytes:
            s = s[0:max_bytes]
        to_function(s)
        max_bytes -= len(s)
        s = from_function(BUFFER_SIZE)
