A detailed explanation of the use of IP address processing ipy module in Python

Source: Internet
Author: User
Tags in python

IPY module can help us to complete the IP calculation and planning. Now there are a lot of IP address computing Web sites, this module to achieve the function and they are similar.

Installation

First download the source code, address: ps://pypi.python.org/pypi/ipy/">https://pypi.python.org/pypi/ipy/, and then unpack using the command Python setup.py install installation.

Use

1. Display IP type

>>> IP (' 192.168.1.1 '). Version ()
4
>>> IP (':: 1 '). Version ()
6
Similar to the above, the version method can be used to determine whether the IP entered is IPv4 or IPV6.

2, network segment calculation output

Code:

From ipy import IP

Ip=ip (' 192.168.0.0/28 ')
Print Ip.len ()
For x in IP:
Print X

Print Ip.strnormal (0)
Print Ip.strnormal (1)
Print Ip.strnormal (2)
Print Ip.strnormal (3)
The Len () method calculates the number of IP segments in the network segment.

The Strnormal () method specifies different Wantprefixlen parameters to customize different types of output. The above output is similar to the following:

16
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
......
192.168.0.15
192.168.0.0
192.168.0.0/28
192.168.0.0/255.255.255.240
192.168.0.0-192.168.0.15
3. Format conversion

Examples of several common methods, including direction resolution name, IP type, IP conversion, network address network segment address translation.

Ip=ip (' 192.168.0.1 ')
Print ip.reversenames () #反向解析地址格式

Print Ip.iptype () #显示IP地址类型, private or public
Ip=ip (' 8.8.8.8 ')
Print Ip.iptype ()

Print Ip.int () #转换成整型格式
Print Ip.strhex () #转换成十六进制格式
Print Ip.strbin () #转换成二进制格式

#网络地址, network segment address format conversion
Print (IP (' 192.168.1.0 '). Make_net (' 255.255.255.0 '))
Print (IP (' 192.168.1.0/255.255.255.0 ', make_net=true))
Print (IP (' 192.168.1.0-192.168.1.255 ', make_net=true))
4, address comparison

Determine if the IP address and network segment are included in another network segment as follows:

>>> ' 192.168.1.1 ' in IP (' 192.168.1.0/24 ')
True
>>> IP (' 192.168.1.0/24 ') in IP (' 192.168.0.0/16 ')
True
Determine if there is overlap in the two network segments, as follows:

>>> IP (' 192.168.0.0/23 '). overlaps (' 192.168.1.0/24 ')
1
>>> IP (' 192.168.1.0/24 '). overlaps (' 192.168.2.0 ')
0
1 indicates overlap, and 0 indicates no overlap.

Example

Code:

#coding: Utf-8

From ipy import IP

Ip_s=raw_input ("Please input an IP or Net-range:")
Ips=ip (ip_s)

If Len (IPs) >1: #网络地址
Print (' Net:%s '% ips.net ())
Print (' netmask:%s '% ips.netmask ())
Print (' Broadcast:%s '% ips.broadcast ())
Print (' Reverse address:%s '% ips.reversenames () [0])
Print (' Subnet:%s '% len (IPS))
else: #单个地址
Print (' Reverse address:%s '% ips.reversenames () [0])

Print (' hexadecimal:%s '% Ips.strhex ())
Print (' binary:%s '% Ips.strbin ())
Print (' Iptype:%s '% Ips.iptype ())
Run Result:

C:\users\admin\workspace\zhangnq>python ipy_test2.py
Please input an IP or net-range:192.168.1.1
Reverse address:1.1.168.192.in-addr.arpa.
Hexadecimal:0xc0a80101
binary:11000000101010000000000100000001
Iptype:private

C:\users\admin\workspace\zhangnq>python ipy_test2.py
Please input an IP or net-range:8.8.8.8
Reverse address:8.8.8.8.in-addr.arpa.
hexadecimal:0x8080808
binary:00001000000010000000100000001000
Iptype:public

C:\users\admin\workspace\zhangnq>python ipy_test2.py
Please input an IP or NET-RANGE:192.168.1.0/28
net:192.168.1.0
netmask:255.255.255.240
broadcast:192.168.1.15
Reverse address:0.1.168.192.in-addr.arpa.
Subnet:16
hexadecimal:0xc0a80100
binary:11000000101010000000000100000000
Iptype:private

IPY Module Usage

A script that automatically identifies information such as IP address, subnet, direction resolution, IP type, etc.

#!/usr/bin/env python
#-*-Coding:utf-8-*-
From ipy import IP # # #加载模块
ip_s = raw_input (' Please enter IP address or network segment address: ') # # # #输入一个IP地址或者网段
ips = IP (ip_s) #定义元素
If Len (IPs) > 1: #如果len出来的数字大于1, then it's a network segment
Print (' Network address:%s '% ips.net ())
Print (' Subnet mask:%s '% ips.netmask ())
Print (' Webcast address:%s '% ips.reversenames () [0])
Print (' Network subnet number:%s '% len (IPS))
else: # # #否则就是一个地址
Print (' IP reverse resolution:%s '% ips.reversenames () [0])
Print (' Hexadecimal address:%s '% Ips.strhex ())
Print (' Binary address:%s '% Ips.strbin ())
Print (' Address type:%s '% Ips.iptype ())
Operation Effect:

[Root@mylinuxer python]# 192.168.1.0/24
-bash:192.168.1.0/24:no such file or directory
[Root@mylinuxer python]# python python.py
Please enter the IP address or network segment address: 192.168.1.0/24
Network address: 192.168.1.0
Subnet Mask: 255.255.255.0
Network broadcast address: 1.168.192.in-addr.arpa.
Number of network subnets: 256

[Root@mylinuxer python]# python python.py
Please enter the IP address or network segment address: 192.168.1.1
IP reverse resolution: 1.1.168.192.IN-ADDR.ARPA.
Hexadecimal address: 0xc0a80101
Binary address: 11000000101010000000000100000001
Address Type: PRIVATE

[Root@mylinuxer python]# python python.py
Please enter the IP address or network segment address: 116.213.249.211
IP reverse resolution: 211.249.213.116.IN-ADDR.ARPA.
Hexadecimal address: 0x74d5f9d3
Binary address: 01110100110101011111100111010011
Address Type: Public

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.