3
3

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 3 years have passed since last update.

Python基礎:Socket、Dnspython

Posted at

#はじめに
nslookup、ドメインからIP取得、IPからドメイン取得などの処理がしたい場合があります。

socket、dnspythonを使って処理できます。

#socket
doc:https://docs.python.org/ja/3/library/socket.html

##ドメインからIPを取得

import socket

print(socket.gethostbyname("google.com"))

172.217.161.78

##ドメインからIPアドレス情報を取得

import socket

print(socket.getaddrinfo("yahoo.co.jp", 443))

[(, 0, 0, '', ('182.22.59.229', 443)), (, 0, 0, '', ('183.79.135.206', 443))]

##IPからFQDNを取得

import socket

print(socket.getfqdn("182.22.59.229"))

f1.top.vip.ssk.yahoo.co.jp

などなど。

#dnspython
doc: http://www.dnspython.org/docs/1.16.0/
##インストール
pip install dnspython

##nslookup
###nslookupコマンド
nslookup yahoo.co.jp

名前: yahoo.co.jp
Addresses: 182.22.59.229
183.79.135.206

###dnspythonでnslookupコマンドと同じ結果を取る

import dns.resolver

print(dns.resolver.query("yahoo.co.jp", "A").response.answer[0])

yahoo.co.jp. 0 IN A 182.22.59.229
yahoo.co.jp. 0 IN A 183.79.135.206

など。

以上

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?