我如何才能创建一个IPv4地址0.0.0.0并对其进行递增,这样它就可以将0-255的输出显示为0.0.0.255,然后是0.0.255.255,0.255.255.255,255.255.255.255?
Avoid using custom functions and go with the netaddr
package. Just create a netaddr.IPNetwork
object for network 0.0.0.0/0
and you should be able to get the nth address by referring it as netaddr.IPNetwork('0.0.0.0/0')[n]
or you can create an iterator as netaddr.IPNetwork('0.0.0.0/0').iter_hosts()
You should be able to get it by doing pip install netaddr
>>> from netaddr import IPNetwork
>>> net = IPNetwork('0.0.0.0/0')
>>> net[1]
IPAddress('0.0.0.1')
>>> str(net[1])
'0.0.0.1'
More examples
>>> str(net[255])
'0.0.0.255'
>>> str(net[256])
'0.0.1.0'
>>> str(net[65535])
'0.0.255.255'
>>> str(net[65536])
'0.1.0.0'
The ipaddress
module in the standard library lets you do arithmetic on IP addresses in the obvious way, and conversion to and from integers, and to and from dotted-quad strings.
I'm not sure what you're trying to do, but whatever it is, it's easy.
For example, here's an iterator over all 4 billion IPv4 addresses:
def all_addrs():
for i in range(0, 1<<32):
yield ipaddress.IPv4Address(i)
Or, if you prefer:
def all_addrs():
addr = ipaddress.IPv4Address(0)
while True:
yield addr
try:
addr += 1
except ValueError:
return
Or, if you want to iterate the first 256, then do three special addresses after that:
def weird_259():
for i in range(256):
yield ipaddress.IPv4Address(i)
yield ipaddress.IPv4Address('0.0.255.255')
yield ipaddress.IPv4Address('0.255.255.255')
yield ipaddress.IPv4Address('255.255.255.255')
If you want to do other things, read the docs for IPv4Address
. Notice that you can construct an address from:
bytes
object of length 4 (most significant octet first). So, let's say you want to make one out the octets 0, 127, 255, 255. How could you do that? First, you create a bytes with those values:
bytes([0, 127, 255, 255])
… and then you create an address with those bytes:
ipaddress.IPv4Address(bytes([0, 127, 255, 255]))
If you want to, you can wrap that up in a function:
def makeaddr(a, b, c, d):
return ipaddress.IPv4Address(bytes([a, b, c, d]))
So, if you wanted the 256 addresses of the form 0.0.0.x
, and the 256 of the form 0.0.x.255
, and 0.x.255.255
, and x.255.255.255
, that's trivial:
def make_1024():
for x in range(256):
yield makeaddr(0, 0, 0, x)
for x in range(256):
yield makeaddr(0, 0, x, 255)
for x in range(256):
yield makeaddr(0, x, 255, 255)
for x in range(256):
yield makeaddr(x, 255, 255, 255)
And if you want to print them out in the default (decimal-dot) notation, just print
them:
for addr in make_1024():
print(addr)
Range from 0 to 16777216 (which is 2 ** 24, as your title requesting for 0.255.255.255
, or 4294967296 which is 2 ** 32 as your question body requesting for 255.255.255.255
), then bitwise AND 0xff
, 0xff00
, 0xff0000
, 0xff000000
to extract the corresponding bits, and shift them right to print with format.
for i in range(0, 2 ** 24): # modify to 2 ** 32 if you want to generate output to 255.255.255.255
byte3 = i & 0xff
byte2 = i & 0xff00
byte1 = i & 0xff0000
byte0 = i & 0xff000000
print('%d.%d.%d.%d' % (byte0 >> 24, byte1 >> 16, byte2 >> 8, byte3))
you can use struct
lib.
import struct
n = 257
y1, y2, y3, y4 = struct.Struct('<I').pack(n & 0xFFFFFFFF)
print("{0}.{1}.{2}.{3}".format(y4,y3,y2,y1)) # OUTPUT: 0.0.1.1
I
stands for big integer (4 bytes), <
stands for big endian (bytes position).
Then you format the bytes in the right order. Increase or decrease n
for having different ip addresses
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.