1.Python IP Address processing ipy module

Source: Internet
Author: User
First download the source code, address: ps: //pypi.python.org/pypi/IPy/ "> https://pypi.python.org/pypi/IPy/, and then decompress it and install using the command python setup.py install

use

1. Display IP type

>>> IP (‘192.168.1.1‘). Version ()
 4
 >>> IP (‘:: 1’). Version ()
 6
Similar to the above, the version method can determine whether the input IP 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 can calculate the number of IPs in the network segment.

The strNormal () method specifies different wantprefixlen parameters to customize different types of output. The output above 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

The example introduces several common methods, including direction resolution name, IP type, IP hexadecimal conversion, and network address segment conversion.

ip = IP (‘192.168.0.1’)
print ip.reverseNames () #Reverse resolution address format

print ip.iptype () #Display IP address type, private or public
ip = IP (‘8.8.8.8’)
print ip.iptype ()

print ip.int () #Convert to integer format
print ip.strHex () #Convert to hexadecimal format
print ip.strBin () #Convert to binary format

#Network address, 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 whether 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 whether the two network segments overlap, 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 means there is overlap, 0 means there is no overlap.

Examples

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: #network address
    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: #Single address
    print (‘reverse address:% s‘% ips.reverseNames () [0])
    
print (‘hexadecimal:% s’% ips.strHex ())
print (‘binary:% s’% ips.strBin ())
print (‘iptype:% s‘% ips.iptype ())
operation 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

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

def ip ():
    try:
        from IPy import IP ### Load module
        ip_s = raw_input (‘Please enter IP address or network segment address:’) ### Enter an IP address or network segment
        ips = IP (ip_s) #define element
        if len (ips)> 1: #If the number from len is greater than 1, then it is a network segment
            print (‘Network address:% s’% ips.net ())
            print (‘Subnet mask:% s’% ips.netmask ())
            print (‘Webcast address:% s’% ips.reverseNames () [0])
            print (‘Number of network subnets:% s’% len (ips))
        else: ### Otherwise it is an address
            print (‘IP reverse resolution:% s’% ips.reverseNames () [0])
            print (‘Hex address:% s’% ips.strHex ())
            print (‘Binary address:% s’% ips.strBin ())
            print (‘Address type:% s’% ips.iptype ())
        print time.strftime ("% Y-% m-% d% H:% M:% S")
        #code
    except Exception, e:
        logging.info ("error:" + str (e) + "\ n" + traceback.format_exc ())
        print traceback.format_exc ()
    finally:
        pass

running result:
 

[[email protected] python] # 192.168.1.0/24
-bash: 192.168.1.0/24: No such file or directory
[[email protected] 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
Webcast address: 1.168.192.in-addr.arpa.
Number of network subnets: 256

[[email protected] 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

[[email protected] python] # python python.py
Please enter the IP address or network segment address: 116.213.249.211
Reverse IP resolution: 211.249.213.116.in-addr.arpa.
Hexadecimal address: 0x74d5f9d3
Binary address: 01110100110101011111100111010011
Address type: PUBLIC

1. IP address processing IPy module in Python

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.