Python3 判断IP是否属于某个网段

# !/usr/bin/python3
# coding: utf-8
import IPy


def in_same_segment(ip, mask, addr):
    """
    ip like 127.0.0.1
    mask like 255.0.0.0
    addr like 192.168.1.1
    """
    # Specifies whether the address is in the network segment
    if '' == addr.strip():
        return True

    subnet = IPy.IP('%s/%s' % (ip, mask), make_net=True)
    if addr not in subnet:
        print('Address %s is not in the segment of %s' % (addr, subnet))
        return False
    return True

 


版权声明:本文为MAOZEXIJR原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。