DNS域名解析

DNS域名系统
DNS是一个分布式的数据库,主要用于吧主机名转换为IP地址。
4.1    获取域名的IP

import sys, socket
# 获取域名的IP地址
result = socket.getaddrinfo(sys.argv[1], None)
print result[0][4]

result = socket.getaddrinfo(sys.argv[1], None)
counter = 0
for item in result:
    print '%-2d:%s' % (counter, item[4])
    counter += 1
 


4.2    根据IP获取域名

import sys, socket

try:
    #  根据IP地址得到域名地址
    result = socket.gethostbyaddr(sys.argv[1])
    print 'Primary hostname:'
    print ' ' + result[0]
    # Display the list of available addressed that is also returned
    print '\nAddress:'
    for item in result[2]:
        print '  ' + item
except socket.herror, e:
    print "Couldn't look up name:", e
 




4.3    获取网络环境信息

import sys, socket

def getipaddrs(hostname):
    result = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM)
    return [x[4][0] for x in result]

# 获取主机名称
hostname = socket.gethostname()
print 'Host name:', hostname

print 'Fully-qualified name:', socket.getfqdn(hostname)
try:
    # 打印IP地址
    print 'IP addressed:', ', '.join(getipaddrs(hostname))
except socket.gaierror, e:
    print 'Could not get IP addressed:', e

 

 

你可能感兴趣的:(socket,UP)