Python basics: Socket, Dnspython

Introduction

You may want to perform processing such as nslookup, IP acquisition from domain, domain acquisition from IP.

It can be processed using socket and dnspython.

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

Get IP from domain

import socket

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

172.217.161.78

Get IP address information from domain

import socket

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

[(<AddressFamily.AF_INET: 2>, 0, 0, '', ('182.22.59.229', 443)), (<AddressFamily.AF_INET: 2>, 0, 0, '', ('183.79.135.206', 443))]

Get FQDN from IP

import socket

print(socket.getfqdn("182.22.59.229"))

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

etc.

dnspython doc: http://www.dnspython.org/docs/1.16.0/

Installation

pip install dnspython

nslookup

nslookup command

nslookup yahoo.co.jp

Name: yahoo.co.jp Addresses: 182.22.59.229 183.79.135.206

Get the same result as the nslookup command in dnspython

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

Such.

that's all

Recommended Posts

Python basics: Socket, Dnspython
Python basics ⑤
Python basics
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Socket communication with Python
Python basics ② for statement
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
# 4 [python] Basics of functions
Basics of python: Output
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Socket communication with Python LEGO Mindstorms
SNS Python basics made with Flask
Python
Getting Started with Python Basics of Python
Review of the basics of Python (FizzBuzz)
Python3 socket module and socket communication flow
Basics of Python x GIS (Part 2)
python documentation reading socket programming HOWTO
About the basics list of Python basics
(python) Deep Learning Library Chainer Basics Basics
Learn the basics of Python ① Beginners
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Statistical basics and Python, graphing, etc. (memo)
[Learning memo] Basics of class by python
Learn the basics while touching python Variables
[JAWS-UG CLI] Lambda Blueprint Explanation: Python2.7 Basics
Python learning basics ~ What is type conversion? ~
Socket communication using socketserver with python now