본문 바로가기

기타

[python] dns(hostname) 정보 확인

728x90

python dns(hostname) 정보 확인

socket.gethostbyname : 도메인 이름에 대한 IP 주소를 반환

socket.gethostbyname('naver.com')

socket.gethostbyname_ex : 확장판(다른 이름의 리스트, 주소의 리스트를 반환)

socket.gethostbyname_ex('naver.com')
$ python
Python 3.9.13 (main, Aug  7 2022, 01:19:39)
[Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyname('naver.com')
'223.130.200.107'
>>> socket.gethostbyname_ex('naver.com')
('naver.com', [], ['223.130.195.200', '223.130.195.95', '223.130.200.104', '223.130.200.107'])
>>>

한 줄로 python 실행

python -c "import socket; print(socket.gethostbyname_ex('sangchul.kr'))"
$ python -c "import socket; print(socket.gethostbyname_ex('sangchul.kr'))"
('sangchul.kr', [], ['27.0.236.139'])

$ python -c "import socket; print(socket.gethostbyname_ex('naver.com'))"
('naver.com', [], ['223.130.195.200', '223.130.195.95', '223.130.200.104', '223.130.200.107'])

$ python
Python 3.9.13 (main, Aug  7 2022, 01:19:39)
[Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.getaddrinfo('sangchul.kr', 80)
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('27.0.236.139', 80)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('27.0.236.139', 80))]
$ python -c "import socket; print(socket.getaddrinfo('sangchul.kr', 80))"
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('27.0.236.139', 80)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('27.0.236.139', 80))]
728x90