def verify_jpeg(datastream):
    """
    To verify if a jpeg image is valid.
    Input:
        datastream, 
    Return:
        bool, True if good, False if something weird happens 
    """
    ENDING = [255, 217]
    try:
        datastream.seek(-2, 2)
    except IOError:
        return False
    for e in ENDING:
        b = datastream.read(1)
        if ord(b) == e:
            continue
        else:
            return False
    return True
