Python通过ip段生成IP地址

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

#!/usr/bin/env python
# -*- coding: utf-8 -*-


def ip2num(ip):
    ip = [int(x) for x in ip.split('.')]
    return ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3]


def num2ip(num):
    return '%s.%s.%s.%s' % (
        (num & 0xff000000) >> 24,
        (num & 0x00ff0000) >> 16,
        (num & 0x0000ff00) >> 8,
        num & 0x000000ff
    )


def gen_ips(start, end):
    """生成IP地址"""
    # if num & 0xff 过滤掉 最后一段为 0 的IP
    return [num2ip(num) for num in range(start, end + 1) if num & 0xff]



转载于:https://my.oschina.net/oncereply/blog/260755

你可能感兴趣的:(Python通过ip段生成IP地址)