how to convert ip address to dot notation

Gerhard Häring gh at ghaering.de
Mon Aug 4 07:06:20 EDT 2003


Ruslan Spivak wrote:

> Hello, python users!
> 
> I have ip addresses in my mysql db in int value - 3232261127, for example.
> How can i convert that to usual dot notation like 192.168.100.7?
> 
> Thanks in advance.
> Your help is very appreciated.

If there's nothing in the standard library, something like

def numIP2strIP(ip):
     l = [str((ip >> 8*n) % 256) for n in range(4)]
     l.reverse()
     return ".".join(l)

should do.

Or you could just use PostgreSQL where you you have a datatype for IP 
addresses :-)

-- Gerhard





More information about the Python-list mailing list