Python randomly generated IP address as string

限于喜欢 提交于 2019-12-18 18:56:30

问题


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?


回答1:


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




回答2:


If you just want a string:

import random

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



回答3:


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

Reference: Fake-Apache-Log-Generator




回答4:


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'



回答5:


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'



回答6:


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



回答7:


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.




回答8:


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'


来源:https://stackoverflow.com/questions/21014618/python-randomly-generated-ip-address-as-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!