Separating IP nodes

Jeff Bauer jbauer at rubic.com
Mon Sep 4 07:05:28 CEST 2000


Kevin Breit wrote:
> In my app, I have a user enter an IP address.  Great.  Now, 
> I want to increment the last number.  For example:
> 1.1.1.1 becomes 1.1.1.2 (yes...thats an odd IP)
> What is the best way to separate that and increment only 
> the last number?

Without error checking ...

>>> from string import join, split
>>> ip = '1.2.3.4'
>>> join(split(ip, '.')[:3] + [str(int(split(ip, '.')[-1]) + 1)], '.')
'1.2.3.5'
>>> 

-Jeff




More information about the Python-list mailing list