[ACCEPTED]-using inet_ntoa function in Python-python
Accepted answer
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
.
from struct import pack
n = pack("!I", 167772160)
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.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.