def quoted(s):
    """ Given a string, return a quoted string as per RFC 3501, section 9."""
    if isinstance(s, str):
        return '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
    else:
        return b'"' + s.replace(b'\\', b'\\\\').replace(b'"', b'\\"') + b'"'