Last updated: Apr 10, 2024
Reading timeยท3 min
To generate a random IP address:
random.randint()
method to generate a random number from 0
to
255
.4
times.str.join()
method to join the numbers with a period separator.from random import randint def generate_random_ip(): return '.'.join( str(randint(0, 255)) for _ in range(4) ) random_ip = generate_random_ip() print(random_ip) # ๐๏ธ 178.166.111.80 random_ip = generate_random_ip() print(random_ip) # ๐๏ธ 124.56.199.70
We used a
generator expression
to iterate over a range
object of length 4.
The range() class is commonly used for looping a specific number of times.
print(list(range(4))) # ๐๏ธ [0, 1, 2, 3]
On each iteration, we use the random.randint()
method to generate a random
number from 0
to 255
.
from random import randint def generate_random_ip(): return '.'.join( str(randint(0, 255)) for _ in range(4) ) random_ip = generate_random_ip() print(random_ip) # ๐๏ธ 178.166.111.80
The
random.randint()
function takes 2 numbers - a
and b
as parameters and returns a random
integer in the range.
Note that the range is inclusive - meaning both a
and b
can be returned.
str()
class to convert each number to a string because the str.join()
method can only be passed string values.The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
The string the method is called on is used as the separator between the elements.
You can use the range()
class to generate a list of N IP addresses.
from random import randint def generate_random_ip(): return '.'.join( str(randint(0, 255)) for _ in range(4) ) a_list = [] for _ in range(3): a_list.append(generate_random_ip()) # ๐๏ธ ['123.31.155.255', '70.22.71.106', '183.142.245.87'] print(a_list)
We used the range
class to call the function 3 times and appended the IP
addresses to a list.
Alternatively, you can use the Faker
module.
This is a three-step process:
Faker
module by running pip install Faker
.Faker
class to create a Faker
object.ipv4()
method to generate a random IP address.from faker import Faker fake = Faker() ip_address = fake.ipv4() print(ip_address) # ๐๏ธ 173.188.6.176 ip_address = fake.ipv4_public() print(ip_address) # ๐๏ธ 54.45.106.198 ip_address = fake.ipv4_private() print(ip_address) # ๐๏ธ 10.103.230.160 ip_address = fake.ipv6() print(ip_address) # ๐๏ธ 120b:ecc2:fe2f:1db1:d79c:99e1:a960:3221
Make sure to install the Faker module if you haven't.
pip install Faker # ๐๏ธ or pip3 pip3 install Faker
The faker.ipv4()
method returns a random IPv4 address.
from faker import Faker fake = Faker() ip_address = fake.ipv4() print(ip_address) # ๐๏ธ 173.188.6.176
The faker.ipv4_public()
method returns a public IPv4 address excluding private
blocks.
from faker import Faker fake = Faker() ip_address = fake.ipv4_public() print(ip_address) # ๐๏ธ 188.104.196.43
The faker.ipv4_private()
returns a private IPv4 address.
from faker import Faker fake = Faker() ip_address = fake.ipv4_private() print(ip_address) # ๐๏ธ 172.18.1.188
The faker.ipv6()
method returns a random IPv6 address.
from faker import Faker fake = Faker() ip_address = fake.ipv6() print(ip_address) # ๐๏ธ 2528:a863:c2ff:413f:b8de:28ff:3f92:5b7
You can learn more about the related topics by checking out the following tutorials: