How to generate a random IP address in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Generate a random IP address in Python
  2. Generate a random IP address using Faker

# Generate a random IP address in Python

To generate a random IP address:

  1. Use the random.randint() method to generate a random number from 0 to 255.
  2. Generate a random number in the range 4 times.
  3. Use the str.join() method to join the numbers with a period separator.
main.py
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

generate random ip address

The code for this article is available on GitHub

We used a generator expression to iterate over a range object of length 4.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

The range() class is commonly used for looping a specific number of times.

main.py
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.

main.py
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.

We used the 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.

# Generating a list of N IP addresses

You can use the range() class to generate a list of N IP addresses.

main.py
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)

generating list of n ip addresses

The code for this article is available on GitHub

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.

# Generate a random IP address using Faker

This is a three-step process:

  1. Install the Faker module by running pip install Faker.
  2. Use the Faker class to create a Faker object.
  3. Use the ipv4() method to generate a random IP address.
main.py
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

generate random ip address using faker

The code for this article is available on GitHub

Make sure to install the Faker module if you haven't.

main.py
pip install Faker # ๐Ÿ‘‡๏ธ or pip3 pip3 install Faker

# Generate random public or private IPv4 addresses

The faker.ipv4() method returns a random IPv4 address.

main.py
from faker import Faker fake = Faker() ip_address = fake.ipv4() print(ip_address) # ๐Ÿ‘‰๏ธ 173.188.6.176
The code for this article is available on GitHub

The faker.ipv4_public() method returns a public IPv4 address excluding private blocks.

main.py
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.

main.py
from faker import Faker fake = Faker() ip_address = fake.ipv4_private() print(ip_address) # ๐Ÿ‘‰๏ธ 172.18.1.188

# Generate random IPv6 addresses

The faker.ipv6() method returns a random IPv6 address.

main.py
from faker import Faker fake = Faker() ip_address = fake.ipv6() print(ip_address) # ๐Ÿ‘‰๏ธ 2528:a863:c2ff:413f:b8de:28ff:3f92:5b7
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev