[ACCEPTED]-using inet_ntoa function in Python-python

Accepted answer
Score: 14

In Python 3.3+ (or with this backport for 2.6 and 2.7), you 3 can simply use ipaddress:

import ipaddress
addr = str(ipaddress.ip_address(167772160))
assert addr == '10.0.0.0'

Alternatively, you can manually 2 pack the value

import socket,struct
packed_value = struct.pack('!I', 167772160)
addr = socket.inet_ntoa(packed_value)
assert addr == '10.0.0.0'

You might also be interested 1 in inet_pton.

Score: 2
from struct import pack

n = pack("!I", 167772160)

0

Score: 0

As a late addition to this thread: if you're 2 using Python 3.3 or above, look at the ipaddress module: it's 1 much more functional than the socket equivalents.

More Related questions