Display stream contents in hexadecimal and ASCII format. The ``stream``
    specified must either be a file-like object that supports the ``read``
    method to receive bytes, or it can be a string.

    To dump a file::

        >>> hexdump(file(filename))     # doctest: +SKIP

    Or to dump stdin::

        >>> import sys
        >>> hexdump(sys.stdin)          # doctest: +SKIP

    :param stream: stream input

def hexdump(stream):
    '''
    Display stream contents in hexadecimal and ASCII format. The ``stream``
    specified must either be a file-like object that supports the ``read``
    method to receive bytes, or it can be a string.

    To dump a file::

        >>> hexdump(file(filename))     # doctest: +SKIP

    Or to dump stdin::

        >>> import sys
        >>> hexdump(sys.stdin)          # doctest: +SKIP

    :param stream: stream input
    '''

    if isinstance(stream, six.string_types):
        stream = BytesIO(stream)

    row = 0
    while True:
        data = stream.read(16)
        if not data:
            break

        hextets = data.encode('hex').ljust(32)
        canonical = printable(data)

        print('%08x %s  %s  |%s|' % (
            row * 16,
            ' '.join(hextets[x:x + 2] for x in range(0x00, 0x10, 2)),
            ' '.join(hextets[x:x + 2] for x in range(0x10, 0x20, 2)),
            canonical,
        ))
        row += 1