Python Forum
IP string manipulation problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IP string manipulation problem
#13
While using Python I very often enjoy standing on shoulders of giants. Thanks to giants it turned out to be very easy-peasy task Big Grin

Using ipaddress module IPv4Address class objects can be converted to int and these integers are consecutive:

In [1]: import ipaddress

In [2]: ipaddress.IPv4Address('10.0.0.0')
Out[2]: IPv4Address('10.0.0.0')

In [3]: int(ipaddress.IPv4Address('10.0.0.0'))
Out[3]: 167772160

In [4]: int(ipaddress.IPv4Address('10.0.0.1'))
Out[4]: 167772161
So getting ip addresses from range is very simple. You get first and last from string representing range and convert the resulting strings to IPv4Address and then to int. Then you just iterate over int range and convert addresses back to string:

import ipaddress

def ip_from_range(ip_range):
    first, last =  [int(ipaddress.IPv4Address(el)) for el in ip_range.split('-')]
    return [str(ipaddress.IPv4Address(i)) for i in range(first, last + 1)]
In [6]: ip_from_range('10.1.1.250-10.1.2.5')
Out[6]:
['10.1.1.250',
 '10.1.1.251',
 '10.1.1.252',
 '10.1.1.253',
 '10.1.1.254',
 '10.1.1.255',
 '10.1.2.0',
 '10.1.2.1',
 '10.1.2.2',
 '10.1.2.3',
 '10.1.2.4',
 '10.1.2.5']
List can potentially be of significant size and generator could be an option. Generator doesn't create list but yields elements one at the time (example is for printing but can be used to write/append to file):

import ipaddress

def ip_from_range(ip_range):
    first, last =  [int(ipaddress.IPv4Address(el)) for el in ip_range.split('-')]
    yield from (str(ipaddress.IPv4Address(i)) for i in range(first, last + 1))

for ip in ip_from_range('10.1.1.250-10.1.2.5'):
    print(ip)

10.1.1.250
10.1.1.251
10.1.1.252
10.1.1.253
10.1.1.254
10.1.1.255
10.1.2.0
10.1.2.1
10.1.2.2
10.1.2.3
10.1.2.4
10.1.2.5
I like Python.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
IP string manipulation problem - by TheRealNoob - Jan-29-2019, 10:03 AM
RE: IP string manipulation problem - by buran - Jan-29-2019, 10:23 AM
RE: IP string manipulation problem - by perfringo - Jan-29-2019, 01:40 PM
RE: IP string manipulation problem - by TheRealNoob - Jan-30-2019, 07:01 AM
RE: IP string manipulation problem - by perfringo - Jan-30-2019, 07:40 AM
RE: IP string manipulation problem - by TheRealNoob - Jan-30-2019, 11:55 AM
RE: IP string manipulation problem - by perfringo - Jan-30-2019, 01:48 PM
RE: IP string manipulation problem - by TheRealNoob - Jan-31-2019, 07:03 AM
RE: IP string manipulation problem - by perfringo - Jan-31-2019, 10:42 AM
RE: IP string manipulation problem - by TheRealNoob - Feb-01-2019, 07:22 AM
RE: IP string manipulation problem - by perfringo - Feb-01-2019, 10:56 AM
RE: IP string manipulation problem - by TheRealNoob - Feb-01-2019, 01:54 PM
RE: IP string manipulation problem - by perfringo - Feb-04-2019, 09:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert string to float problem vasik006 8 3,429 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Problem in list manipulation CyKlop 6 2,328 Oct-18-2021, 09:03 AM
Last Post: DeaD_EyE
  f string concatenation problem growSeb 3 2,276 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 2,712 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 1,984 Jan-05-2021, 01:33 PM
Last Post: perfringo
  optimization problem for dataframe manipulation fimmu 0 1,475 Aug-31-2020, 06:02 PM
Last Post: fimmu
  string problem Mathisdlg 6 2,878 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 3,053 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 3,682 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Problem with inserting a string in to Sqlite db darktitan 3 4,528 Mar-03-2019, 06:30 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020