Separating IP nodes
Brad Knotwell
knotwell at ix.netcom.com
Sun Sep 3 03:59:12 EDT 2000
"John W. Baxter" <jwbnews at scandaroon.com> writes:
> My temptation would be to convert the dotted string form of the IP
> address to a (long) integer, catching errors, add one, and convert back
> to a dotted string (catching the error that the user put in
> 255.255.255.255 which has become the pseudo IP 1.0.0.0.0).
>
> Functions (methods) to do this are in...(oops, as I've never needed
> them, I had to try to find the code...and I've failed so far).
Here's a good start (NOTE: they assume a reasonable input)
def ipv4_cvt((first,second,third,fourth)):
return (first << 24L) + (second << 16) + (third << 8) + fourth
def ipv4_uncvt2(addr,shift = (24,16,8,0),res = ()):
octet = (addr >> shift[0])
res = res + (octet,)
if not shift[0]:
return res
else:
return ip_uncvt2(addr - (octet * (256L << shift[1])),shift[1:], res)
maxaddr = ipv4_cvt([255,255,255,255])
myaddr = ipv4_cvt([10,100,200,255])
if myaddr >= maxaddr:
raise 'BadIpAddress'
else:
print ip_uncvt(myaddr+1)
> John W. Baxter Port Ludlow, WA USA jwbnews at scandaroon.com
--Brad
More information about the Python-list
mailing list