inet_pton.py 405 Bytes
Newer Older
1 2 3 4
#!/usr/bin/env python3
"""
Print IP address to binary representation in Python hex format \x12
"""
5

6
from socket import inet_pton, AF_INET6, AF_INET
7 8 9
import sys
from binascii import hexlify

10 11 12 13
try:
    arg = sys.argv[1]
except:
    sys.exit('Usage: inet_pton.py <IPv4 or IPv6 address>')
14

15 16 17 18
if ':' in arg:
    addr_type = AF_INET6
else:
    addr_type = AF_INET
19

20
print(repr(inet_pton(addr_type, arg)).strip("'"))