简体   繁体   English

Python:可寻址 IP 地址列表

[英]Python: List of addressable IP addresses

What's the most Pythonic way to create a list of the addressable IP addresses given a netaddr IPRange or netaddr IPNetwork.在给定 netaddr IPRange 或 netaddr IPNetwork 的情况下,创建可寻址 IP 地址列表的最 Pythonic 方法是什么?

If I use these, then it includes subnet and broadcast addresses:如果我使用这些,那么它包括子网和广播地址:

hosts = list(IPRange('212.55.64.0','212.55.127.255'))
hosts = IPNetwork('192.168.0.1/24')

So what I need for say IPNetwork(192.168.0.0/27) is a list from 192.168.0.1 to 192.168.0.31 note that 192.168.0.0 and 192.168.0.32 must not be included.所以我需要说的 IPNetwork(192.168.0.0/27) 是一个从 192.168.0.1 到 192.168.0.31 的列表,注意不能包括 192.168.0.0 和 192.168.0.32。

EDIT编辑

Thanks for info on how to do it with IPy.感谢您提供有关如何使用 IPy 进行操作的信息。 Does anybody know if it can be done with netaddr?有谁知道是否可以用netaddr完成?

The following is a quick script to do what you want using netaddr 以下是使用netaddr执行所需操作的快速脚本
(Python 2.7, linux) (Python 2.7,linux)

from netaddr import *

def addr(address, prefix):
    ip = IPNetwork(address)
    ip.prefixlen = int(prefix)
    return ip

myip = addr('192.168.0.0', '27')

for usable in myip.iter_hosts():
    print '%s' % usable

IPY

经过一番研究后我发现了这种方式:

 l = list(netaddr.IPNetwork('192.168.0.0/27').iter_hosts())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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