Random IP Address Generator in Python

Random IP Address Generator in Python

  1. Use the Faker Module to Generate Random IP Address as String in Python
  2. Use the random Module to Generate Random IP Address as String in Python
  3. Use the ipaddress Module to Generate Random IP Address as String in Python
  4. Use the socket Module to Generate Random IP Address as String in Python

An IP (Internet Protocol) address is a unique number that identifies a device on the network. IPv4 and IPv6 are two versions of IP addresses.

An IPv4 address is separated into four parts by dots ., and IPv6 is separated by semicolons : into eight parts.

Python provides multiple modules that can be used to print a random IP address. This tutorial will teach you to generate a random IP address as a string in Python.

Use the Faker Module to Generate Random IP Address as String in Python

Faker is a Python module that allows you to generate fake data. It can print different types of fake data, including IP address, name, email, country, text, etc.

You can install Faker using the pip command.

pip install Faker

The following example generates a random IPv4 address in Python.

from faker import Faker
fake = Faker()
ip_addr = fake.ipv4()
print(ip_addr)

Output:

126.144.73.38

To print a random IPv6 address, you can run the script below.

from faker import Faker
fake = Faker()
ip_addr = fake.ipv6()
print(ip_addr)

Output:

f0d7:7e1e:7a39:32f1:c4aa:1e80:2287:1311

Use the random Module to Generate Random IP Address as String in Python

The random is a built-in module in Python to print random numbers.

The following example generates a random IPv4 address in Python.

import random

ip = ".".join(str(random.randint(0, 255)) for _ in range(4))
print(ip)

Output:

60.254.193.222

Use the ipaddress Module to Generate Random IP Address as String in Python

The ipaddress module in Python provides functionalities to generate, manipulate, and work on IPv4 and IPv6 addresses and networks.

The following example prints a random IPv4 address in Python.

import ipaddress
import random

IPV4 = ipaddress.IPv4Address._ALL_ONES
def random_ipv4():
    return  ipaddress.IPv4Address._string_from_ip_int(
        random.randint(0, IPV4)
    )
print(random_ipv4())

Output:

114.71.143.104

To generate an IPv6 address, execute this script.

import ipaddress
import random

IPV6 = ipaddress.IPv6Address._ALL_ONES
def random_ipv6():
    return ipaddress.IPv6Address._string_from_ip_int(
        random.randint(0, IPV6)
    )
print(random_ipv6())

Output:

44c3:48f4:669a:e964:6c93:75ca:3625:59d3

Use the socket Module to Generate Random IP Address as String in Python

This is another method to generate an IPv4 address using random, socket, and struct modules in Python.

import random
import socket
import struct
ip=socket.inet_ntoa(struct.pack('>I', random.randrange(1, 0xffffffff)))
print(ip)

Output:

101.131.185.15

You have learned to use different Python modules to print both IPv4 and IPv6 addresses. Now you should know how to generate a random IP address as a string in Python.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

Related Article - Python IP Address

  • Python Validate IP Address