Skip to content

Instantly share code, notes, and snippets.

@kot-behemoth
Created March 6, 2018 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kot-behemoth/74a8c3304a3834ae61085d867f55f980 to your computer and use it in GitHub Desktop.
Save kot-behemoth/74a8c3304a3834ae61085d867f55f980 to your computer and use it in GitHub Desktop.
import itertools
from ipaddress import ip_address
all_ips_tuples = itertools.product(range(256), repeat=4)
# these are generators, meaning they don't store all values in memory
ip_strings = ('.'.join(map(str, ip_tuple))
for ip_tuple in all_ips_tuples)
ip_objects = (ip_address(ip_string)
for ip_string in ip_strings)
# get an IP object
next(ip_objects)
# get all IP objects - warning, consumes a lot of memory
all_ip_objects = list(ip_objects)
@kot-behemoth
Copy link
Author

Examples

next(ip_strings)
Out[136]: '0.0.0.1'

In [137]: next(ip_strings)
Out[137]: '0.0.0.2'

In [138]: next(ip_objects)
Out[140]: IPv4Address('0.0.0.3')

In [141]: next(ip_objects)
Out[141]: IPv4Address('0.0.0.4')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment