我正在尝试编写一个脚本,该脚本将通过命令行参数接收主机名并执行dns查询。此外,它需要检查命名的进程是否正在运行如果查询成功,则不会发生任何事情。如果无法解析主机,则会抛出异常。
我从一本书开始研究以下示例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import sys
def main():
DNS.DiscoverNameServers()
request = DNS.Request()
try:
for qt in DNS.Type.A, DNS.Type.AAAA, DNS.Type.CNAME, DNS.Type.MX, DNS.Type.NS:
reply = request.req(name=sys.argv[1], qtype=qt)
for answer in reply.answers:
print answer['name'], answer['classstr'], answer['typename'], \
repr(answer['data'])
except DNSError as e:
raise e
if __name__=="__main__":
main()