Search code examples
pythonpython-2.7ipconvertersoctal

Python Convert IP to octal address


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('.')])

Solution

  • 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