python socket模块打印dns查询的例子

发布时间:2019-12-26编辑:脚本学堂
分享一个python socket模块实现打印dns查询的小例子,学习下python模块socket的用法,有需要的朋友参考下。

python socket,python模块,dns查询

例子,python socket模块打印dns查询。

代码:
 

复制代码 代码示例:

#!/usr/bin/python
#site: www.jb200.com
#
import socket

def gethost_ex(host_ex):
        hostname_ex,alias_ex,address_ex = socket.gethostbyname_ex(host_ex)
        return hostname_ex,alias_ex,address_ex
def array_print(*arr):
        for item in arr:
                print '  ',item
        print

for host in ["www.baidu.com","www.jb200.com","www.google.com"]:
        print host
        try :
                hostname,alias,address = gethost_ex(host)
        except socket.error as e:
                print 'error',e
                continue
        print ' HostName:',hostname
        print ' Alias:'
        array_print(*alias)
        print ' Address:'
        array_print(*address)