Python随机生成的IP地址作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21014618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:47:49  来源:igfitidea点击:

Python randomly generated IP address as string

pythonip-address

提问by changzhi

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

在Python中,如果我想生成一个IP地址形式的随机字符串怎么办?

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

例如:"10.0.1.1""10.0.3.14""172.23.35.1"等。

Could someone give me some help?

有人能给我一些帮助吗?

采纳答案by falsetru

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

NOTEThis could generate IPs like 0.0.0.0, 255.255.255.255.

注意这可能会生成像0.0.0.0255.255.255.255.

回答by jonrsharpe

If you just want a string:

如果你只想要一个字符串:

import random

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

回答by zhangxaochen

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'

回答by Halil Kaskavalci

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

这可能太明显了,但是如果您需要某个范围内的随机 IP,您可以使用这个:

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

回答by DmitrySemenov

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

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.

lib 有很多其他有用的选项来伪造数据。

回答by Sandeep Kanabar

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

Reference: Fake-Apache-Log-Generator

参考:Fake-Apache-Log-Generator

回答by coder

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

另一种以 IP 地址形式生成随机字符串的方法是:

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

回答by Brad Solomon

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

您还可以使用 Python 的ipaddress模块,更广泛地用于创建、操作和操作 IPv4 和 IPv6 地址和网络:

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'