import struct
import socket
import binascii


def compact(ip, port, ascii=False):
	"""
	Compact IP address and port.

	>>> compact('127.0.0.1', 6667, ascii=True)
	'7f0000011a0b'
	>>> compact('127.0.0.1', 6667) == '7f0000011a0b'.decode('hex')
	True
	"""
	compacted = struct.pack('!4sH', socket.inet_aton(ip), port)
	return binascii.hexlify(compacted) if ascii else compacted