Python's computational-ipy on IP

Source: Internet
Author: User

At work, we sometimes encounter the need to calculate whether an IP or network segment is in another network segment. Sometimes it is judged whether the two segments overlap. This is a lot easier to use ipy.



The following shell is the installation of ipy:

Cd/usr/localwget https://pypi.python.org/packages/source/I/IPy/IPy-0.81.tar.gz--NO-CHECK-CERTIFICATETAR-ZXVF IPY-0.81.TAR.GZCD Ipy-0.81python setup.py Install

The convenience of Ipy:

1:ip (' 10.0.0.0/8 '). Version () #获取IP的类型

2:

ip = IP (' 192.168.0.0/16 ') print Ip.len () #有多少个ipfor x in Ip:print (x) #分别打印出每个ip

3:

IP (' 8.8.8.8 '). Iptype () #ip的类型IP (' 8.8.8.8 '). Int () #转换成整形IP (' 8.8.8.8 '). Strhex () #转换成十六进制IP (' 8.8.8.8 '). Strbin () # Convert to binary print (IP (0x8080808)) #十六进制转换成ip

4:

IP (' 192.168.1.0/24 '). Strnormal (1) #输出/24 format IP (' 192.168.1.0/24 '). Strnormal (2) #输出掩码格式IP (' 192.168.1.0/24 '). Strnormal (3) #输出广播地址格式

5:

IP (' 192.168.1.0/24 ') in IP (' 192.168.0.0/16 ') #192.168.1.0/24 is inside the 192.168.0.0/16 bit

6:

IP (' 192.168.1.0/24 '). overlaps (' 192.168.2.0/24 ') #网段不存在重叠 returns 0 (returns 0 if overlapping)


Through the above record, we can solve the problem in the work is: batch to determine the type of IP, the bulk of the IP into their desired format, batch to determine whether an IP is included in a network segment, in batches to determine whether there is overlap between two network segments.

This article is from the "Zhangweihong" blog, make sure to keep this source http://zhuangweihong.blog.51cto.com/8808431/1650093

Python's computational-ipy on IP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.