Python randomly generated IP address as string

前端 未结 9 2076
遥遥无期
遥遥无期 2020-12-29 23:14

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\",

相关标签:
9条回答
  • 2020-12-29 23:39

    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
    
    0 讨论(0)
  • 2020-12-29 23:40
    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'
    
    0 讨论(0)
  • 2020-12-29 23:44
    >>> 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.

    0 讨论(0)
  • 2020-12-29 23:47

    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.

    0 讨论(0)
  • 2020-12-29 23:48
    from faker import Faker  
    faker = Faker()  
    ip_addr = faker.ipv4()  
    

    Reference: Fake-Apache-Log-Generator

    0 讨论(0)
  • 2020-12-29 23:55

    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'
    
    0 讨论(0)
提交回复
热议问题