<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_essid(interface, essid):
 """ Set the ESSID of a given interface Arguments: interface - device to work on (e.g. eth1, wlan0). essid - ESSID to set. Must be no longer than IW_ESSID_MAX_SIZE (typically 32 characters). """

    interface = _get_bytes(interface)
    essid = _get_bytes(essid)

    wrq = ffi.new('struct iwreq*')

    with iwlib_socket() as sock:
        if essid.lower() in (b'off', b'any'):
            wrq.u.essid.flags = 0
            essid = b''
        elif essid.lower() == b'on':
            buf = ffi.new('char []', iwlib.IW_ESSID_MAX_SIZE+1)
            wrq.u.essid.pointer = buf
            wrq.u.essid.length = iwlib.IW_ESSID_MAX_SIZE + 1
            wrq.u.essid.flags = 0
            if iwlib.iw_get_ext(sock, interface, iwlib.SIOCGIWESSID, wrq) < 0:
                raise ValueError("Error retrieving previous ESSID: %s" % (os.strerror(ffi.errno)))
            wrq.u.essid.flags = 1
        elif len(essid) > iwlib.IW_ESSID_MAX_SIZE:
            raise ValueError("ESSID '%s' is longer than the maximum %d" % (essid, iwlib.IW_ESSID_MAX_SIZE))
        else:
            wrq.u.essid.pointer = ffi.new_handle(essid)
            wrq.u.essid.length = len(essid)
            wrq.u.essid.flags = 1

        if iwlib.iw_get_kernel_we_version() < 21:
            wrq.u.essid.length += 1

        if iwlib.iw_set_ext(sock, interface, iwlib.SIOCSIWESSID, wrq) < 0:
            errno = ffi.errno
            strerror = "Couldn't set essid on device '%s': %s" % (interface.decode('utf8'), os.strerror(errno))
            raise OSError(errno, strerror)