0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

小道具:ホスト名からIPアドレスを得る(getaddrinfo の動作確認)

Posted at

getaddrinfo の動作確認用です

getaddrinfo を使用して、ホスト名に対しする IP アドレスを表示します。

getaddrinfo.py
#!/usr/bin/env python3

import socket
import argparse


def get_socket_param(name):
    if name in socket.__dict__:
        return socket.__dict__[name]
    return int(name)


def get_socket_name(prefix, value):
    nprefix = len(prefix)
    for key in socket.__dict__:
        if (len(key) > nprefix and
            key[:nprefix] == prefix and
                socket.__dict__[key] == value):
            return key
    return f'{value}'


parser = argparse.ArgumentParser()
parser.add_argument('host')
parser.add_argument('-N', '--port', help='ポート番号を指定')
parser.add_argument('-A', '--family', help='AF_ に続く名前を指定')
parser.add_argument('-T', '--type', help='SOCK_ に続く名前を指定')
parser.add_argument('-P', '--proto', help='IPPROTO_ に続く名前を指定')
parser.add_argument('flag', nargs='*', help='AI_ に続くフラグ名前を指定')
args = parser.parse_args()

host = args.host
port = 0 if not args.port else int(args.port)
af_family = 0 if not args.family else get_socket_param('AF_' + args.family.upper())
sock_type = 0 if not args.type else get_socket_param('SOCK_' + args.type.upper())
ipproto = 0 if not args.proto else get_socket_param('IPPROTO_' + args.proto.upper())
flags = sum(get_socket_param('AI_' + flag.upper()) for flag in (args.flag or []))

param = ', '.join([f'host="{host}"',
                   f'port={port}',
                   'family=%s' % get_socket_name('AF_', af_family),
                   'type=%s' % get_socket_name('SOCK_', sock_type),
                   'proto=%s' % get_socket_name('IPPROTO_', ipproto),
                   f'flags=0x{flags:02x}'])

print(f'getaddrinfo({param}):')
addrinfo = socket.getaddrinfo(host, port, af_family, sock_type, ipproto, flags)
print('\n'.join(ai[4][0] for ai in addrinfo))
実行結果
$ python3 getaddrinfo.py example.com
getaddrinfo(host="example.com", port=0, family=AF_UNSPEC, type=0, proto=IPPROTO_IP, flags=0x00):
2606:2800:220:1:248:1893:25c8:1946
2606:2800:220:1:248:1893:25c8:1946
93.184.216.34
93.184.216.34
$ python3 getaddrinfo.py example.com -A inet
getaddrinfo(host="example.com", port=0, family=AF_INET, type=0, proto=IPPROTO_IP, flags=0x00):
93.184.216.34
93.184.216.34
$ python3 getaddrinfo.py example.com -A inet6
getaddrinfo(host="example.com", port=0, family=AF_INET6, type=0, proto=IPPROTO_IP, flags=0x00):
2606:2800:220:1:248:1893:25c8:1946
2606:2800:220:1:248:1893:25c8:1946
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?