IP address to binary conversion

Rikard Bosnjakovic bos at hack.org
Fri Jun 15 02:57:47 EDT 2001

Jeroen Wolff wrote:

> Is there a way back to present the calculated results in a doted
> decimal notation?

Greg's code:

>>> import socket, struct
>>> ip = ""
>>> q = ip.split(".")
>>> n = reduce(lambda a,b: long(a)*256 + long(b), q)
>>> n

Back to dotted:

>>> socket.inet_ntoa(struct.pack('!I', n))

Rikard Bosnjakovic - http://bos.hack.org/cv/ - ICQ: 1158217

Anyone sending unwanted advertising e-mail to my address will be
charged $250 for network traffic and computing time. By extracting my
address from this message or its header, you agree to these terms.

More information about the Python-list mailing list