简体   繁体   中英

Python randomly generated IP address as string

In Python, what should I do if I want to generate a random string in the form of an IP address?

For example: "10.0.1.1" , "10.0.3.14" , "172.23.35.1" and so on.

Could someone give me some help?

>>> import random
>>> import socket
>>> import struct
>>> socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
'197.38.59.143'
>>> socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
'228.237.175.64'

NOTE This could generate IPs like 0.0.0.0 , 255.255.255.255 .

If you just want a string:

import random

ip = ".".join(map(str, (random.randint(0, 255) 
                        for _ in range(4))))
from faker import Faker  
faker = Faker()  
ip_addr = faker.ipv4()  

Reference: Fake-Apache-Log-Generator

In [123]: '.'.join('%s'%random.randint(0, 255) for i in range(4))
Out[123]: '45.204.56.200'

In [124]: '.'.join('%s'%random.randint(0, 255) for i in range(4))
Out[124]: '7.112.222.205'

An alternative way to generate a random string in the form of an IP address is:

>>> ip = '{}.{}.{}.{}'.format(*__import__('random').sample(range(0,255),4))
>>> ip
'45.162.105.102'

It may be too obvious but if you need random IPs within a range you can use this:

import random

for x in xrange(1,100):
  ip = "192.168."
  ip += ".".join(map(str, (random.randint(0, 255) 
                          for _ in range(2))))

  print ip

https://faker.readthedocs.io/en/latest/providers/faker.providers.internet.html

import faker
fake = Factory.create()
ip_addr = fake.ipv4(network=False)

lib has a lot of other useful options to fake data.

You also have Python's ipaddress module available to you, useful more broadly for creating, manipulating and operating on IPv4 and IPv6 addresses and networks:

import ipaddress
import random

MAX_IPV4 = ipaddress.IPv4Address._ALL_ONES  # 2 ** 32 - 1
MAX_IPV6 = ipaddress.IPv6Address._ALL_ONES  # 2 ** 128 - 1


def random_ipv4():
    return  ipaddress.IPv4Address._string_from_ip_int(
        random.randint(0, MAX_IPV4)
    )

def random_ipv6():
    return ipaddress.IPv6Address._string_from_ip_int(
        random.randint(0, MAX_IPV6)
    )

Examples:

>>> random.seed(444)                                                                                                                                                                                                                                         
>>> random_ipv4()                                                                                                                                                                                                                                            
'79.19.184.109'
>>> random_ipv4()                                                                                                                                                                                                                                            
'3.99.136.189'
>>> random_ipv4()                                                                                                                                                                                                                                            
'124.4.25.53'
>>> random_ipv6()                                                                                                                                                                                                                                            
'4fb7:270d:8ba9:c1ed:7124:317:e6be:81f2'
>>> random_ipv6()                                                                                                                                                                                                                                            
'fe02:b348:9465:dc65:6998:6627:1300:29c9'
>>> random_ipv6()                                                                                                                                                                                                                                            
'74a:dd88:1ff2:bfe3:1f3:81ad:debd:db88'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM