def text_type(s):
    """
    Given an unnamed piece of data, try to guess its content type.

    Detects HTML, XML, and plain text.

    :return: A MIME type string such as 'text/html'.
    :rtype: str

    :param bytes s: The binary data to examine.
    """
    # at least the maximum length of any tags we look for
    max_tags = 14
    s2 = s.strip()[:max_tags].lower()

    if len(s2) == max_tags:
        if s2.startswith(b'<html>'):
            return 'text/html'

        if s2.startswith(b'<!doctype html'):
            return 'text/html'

        # what about encodings??
        if s2.startswith(b'<?xml'):
            return 'text/xml'

    # we also recognize small snippets of HTML - the closing tag might be
    # anywhere, even at the end of
    if b'</' in s:
        return 'text/html'

    return 'text/plain'