http://docs.python.jp/3/library/ipaddress.html Hinzugefügt in Python 3.3. Es gibt einen Backport.
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'
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'
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