Reputation: 13
how to convert IP address to dotted octal address in python 2.7 ? example something like this ': 127.0.0.1 = 0177.0000.0000.0001'
i have try this but give me different result
ip = '127.0.0.1'
print '.'.join([oct(int(x)+256)[3:] for x in ip.split('.')])
Upvotes: 1
Views: 987
Reputation: 3503
Try this:
ip="127.0.0.1"
octalIP='.'.join(["%04o" % int(x) for x in ip.split('.')])
print octalIP
output: 0177.0000.0000.0001
Upvotes: 0
Reputation: 2967
Try this:
[EDIT: Thanks to abarnert for pointing out a simpler way to use format]
>>> ip = '127.0.0.1'
>>> print '.'.join(format(int(x), '04o') for x in ip.split('.'))
0177.0000.0000.0001
Upvotes: 1
Reputation: 11430
Try this:
print '%04o.%04o.%04o.%04o' % tuple(map(int, ip.split('.')))
Upvotes: 1