Python ipaddress package notes

ipaddress package

http://docs.python.jp/3/library/ipaddress.html Added in Python 3.3. There is a back port.

Determine whether remote_host is "IPv4 address", "IPv6 address", or "Other".

python


import ipaddress

def type_of_remote_host(remote_host):
    try:
        addr = ipaddress.ip_address(unicode(address))
        if addr.version == 4:
            return 'ipv4addr'
        else:
            return 'ipv6addr'

    except ValueError:
        return 'other'

Redundant and abbreviated mutual conversion of IPv6 addresses

python


import ipaddress

ipaddress.ip_address(u'2001:0db8:0000:0000:0000:0000:0000:9abc').compressed
# => u'2001:db8::9abc'

ipaddress.ip_address(u'2001:db8::9abc').exploded
# => '2001:0db8:0000:0000:0000:0000:0000:9abc'

Get the host name for reverse lookup from the IP address

python


import ipaddress

## IPv4
'.'.join(reversed('192.168.0.1'.split('.')))+'.in-addr.arpa.'
# => '1.0.168.192.in-addr.arpa.'

## IPv6
'.'.join(reversed(ipaddress.ip_address(u'2001:db8::9abc').exploded.replace(':','')))+'.ip6.arpa.'
# => 'c.b.a.9.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.'

Recommended Posts

Python ipaddress package notes
Python scraping notes
Python study notes _000
Python learning notes
Python beginner notes
Python study notes_006
python C ++ notes
Python study notes _005
Python grammar notes
Python Library notes
python personal notes
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
missingintegers python personal notes
Python test package memo
python decorator usage notes
[Personal notes] Python, Django
Python Pickle format notes
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Matlab => Python migration notes
Python package manager comparison
Notes around Python3 assignments
Notes using Python subprocesses
Anaconda Package Management Notes
Python try / except notes
python> Link> PyUserInput package |
Python framework bottle notes
Python notes using perl-ternary operator
O'Reilly python3 Primer Learning Notes
Web scraping notes in python3
Python package management in IntelliJ
Python notes to forget soon
python * args, ** kwargs Usage notes
Python notes using perl-special variables
Python 處 處 regular expression Notes
Python Tkinter notes (for myself)
[Python] Notes on data analysis
Python data analysis learning notes
Notes on installing Python on Mac
Get Evernote notes in Python
Notes on installing Python on CentOS
A record of patching a python package
Notes on Python and dictionary types
Notes on package management with conda
Python
Minimum grammar notes for writing Python
Notes on using MeCab from Python
Bayesian optimization package GPyOpt in Python
Personal notes for python image processing
Python package management tool personal summary
Visualize python package dependencies with graphviz
Python Pandas Data Preprocessing Personal Notes
Typing automation notes by Python beginners
Notes for me python csv graph
Notes on installing Python using PyEnv
Notes for Python file input / output
Notes on using rstrip with python.
Notes on accessing dashDB from python