{"id":965067,"date":"2024-12-27T04:34:26","date_gmt":"2024-12-26T20:34:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/965067.html"},"modified":"2024-12-27T04:34:28","modified_gmt":"2024-12-26T20:34:28","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96dns%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/965067.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6dns\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181541\/c7f36c53-81b2-4cf9-86d5-0f359a795cb0.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6dns\u4fe1\u606f\" \/><\/p>\n<p><p> \u4e00\u3001\u5f00\u5934\u6bb5\u843d<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u83b7\u53d6DNS\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>socket\u5e93<\/strong>\u3001<strong>dnspython\u5e93<\/strong>\u3001<strong>subprocess\u6a21\u5757<\/strong>\u7b49\u65b9\u6cd5\u3002<strong>socket\u5e93<\/strong>\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\uff0c\u5176\u4e2d\u7684<code>gethostbyname()<\/code>\u548c<code>gethostbyaddr()<\/code>\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u57fa\u672c\u7684DNS\u4fe1\u606f\u3002<strong>dnspython\u5e93<\/strong>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u652f\u6301\u590d\u6742\u7684DNS\u67e5\u8be2\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6df1\u5165\u4e86\u89e3DNS\u7ed3\u6784\u7684\u7528\u6237\u3002<strong>subprocess\u6a21\u5757<\/strong>\u53ef\u4ee5\u8c03\u7528\u7cfb\u7edf\u7684DNS\u5de5\u5177\uff0c\u5982<code>nslookup<\/code>\u6216<code>dig<\/code>\uff0c\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684DNS\u67e5\u8be2\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528SOCKET\u5e93\u83b7\u53d6DNS\u4fe1\u606f<\/p>\n<\/p>\n<p><p>Python\u7684socket\u5e93\u662f\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u7840\u529f\u80fd\u7528\u4e8e\u5904\u7406DNS\u4fe1\u606f\u7684\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528gethostbyname()\u83b7\u53d6IP\u5730\u5740<\/h3>\n<\/p>\n<p><p><code>gethostbyname()<\/code>\u662fsocket\u5e93\u4e2d\u7684\u4e00\u4e2a\u7b80\u5355\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u7ed9\u5b9a\u4e3b\u673a\u540d\u7684IPv4\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def get_ip_by_hostname(hostname):<\/p>\n<p>    try:<\/p>\n<p>        ip_address = socket.gethostbyname(hostname)<\/p>\n<p>        print(f&quot;IP address of {hostname} is {ip_address}&quot;)<\/p>\n<p>    except socket.error as err:<\/p>\n<p>        print(f&quot;Cannot resolve {hostname}: {err}&quot;)<\/p>\n<p>get_ip_by_hostname(&quot;www.example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528gethostbyaddr()\u83b7\u53d6\u4e3b\u673a\u540d<\/h3>\n<\/p>\n<p><p><code>gethostbyaddr()<\/code>\u53ef\u4ee5\u901a\u8fc7IP\u5730\u5740\u83b7\u53d6\u76f8\u5173\u7684\u4e3b\u673a\u540d\u548c\u5176\u4ed6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_hostname_by_ip(ip):<\/p>\n<p>    try:<\/p>\n<p>        host_info = socket.gethostbyaddr(ip)<\/p>\n<p>        print(f&quot;Hostname for IP {ip} is {host_info[0]}&quot;)<\/p>\n<p>    except socket.error as err:<\/p>\n<p>        print(f&quot;Cannot resolve IP {ip}: {err}&quot;)<\/p>\n<p>get_hostname_by_ip(&quot;93.184.216.34&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528DNSPYTHON\u5e93\u8fdb\u884c\u9ad8\u7ea7\u67e5\u8be2<\/p>\n<\/p>\n<p><p>dnspython\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684DNS\u67e5\u8be2\u64cd\u4f5c\uff0c\u6bd4\u5982A\u8bb0\u5f55\u3001MX\u8bb0\u5f55\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u4e0e\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5dnspython\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install dnspython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u5b83\u8fdb\u884cDNS\u67e5\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dns.resolver<\/p>\n<p>def query_dns(dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n):<\/p>\n<p>    try:<\/p>\n<p>        result = dns.resolver.resolve(domain, &#39;A&#39;)<\/p>\n<p>        for ipval in result:<\/p>\n<p>            print(f&quot;IP address for {domain} is {ipval.to_text()}&quot;)<\/p>\n<p>    except dns.resolver.NoAnswer:<\/p>\n<p>        print(f&quot;No answer for {domain}&quot;)<\/p>\n<p>    except dns.resolver.NXDOMAIN:<\/p>\n<p>        print(f&quot;{domain} does not exist&quot;)<\/p>\n<p>query_dns(&quot;www.example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u67e5\u8be2MX\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>MX\u8bb0\u5f55\u7528\u4e8e\u90ae\u4ef6\u670d\u52a1\u5668\u7684\u67e5\u8be2\uff0cdnspython\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u6b64\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def query_mx_record(domain):<\/p>\n<p>    try:<\/p>\n<p>        mx_records = dns.resolver.resolve(domain, &#39;MX&#39;)<\/p>\n<p>        for mx in mx_records:<\/p>\n<p>            print(f&quot;MX Record: {mx.exchange} with priority {mx.preference}&quot;)<\/p>\n<p>    except dns.resolver.NoAnswer:<\/p>\n<p>        print(f&quot;No MX record found for {domain}&quot;)<\/p>\n<p>query_mx_record(&quot;example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u67e5\u8be2NS\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>NS\u8bb0\u5f55\u7528\u4e8e\u6307\u5b9a\u57df\u540d\u670d\u52a1\u5668\u7684\u5730\u5740\uff0c\u4ee5\u4e0b\u662f\u67e5\u8be2NS\u8bb0\u5f55\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def query_ns_record(domain):<\/p>\n<p>    try:<\/p>\n<p>        ns_records = dns.resolver.resolve(domain, &#39;NS&#39;)<\/p>\n<p>        for ns in ns_records:<\/p>\n<p>            print(f&quot;NS Record: {ns.target}&quot;)<\/p>\n<p>    except dns.resolver.NoAnswer:<\/p>\n<p>        print(f&quot;No NS record found for {domain}&quot;)<\/p>\n<p>query_ns_record(&quot;example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528SUBPROCESS\u6a21\u5757\u8c03\u7528\u7cfb\u7edf\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf\u5de5\u5177\u5982<code>nslookup<\/code>\u6216<code>dig<\/code>\u53ef\u80fd\u66f4\u65b9\u4fbf\u3002\u53ef\u4ee5\u901a\u8fc7subprocess\u6a21\u5757\u5728Python\u4e2d\u8c03\u7528\u8fd9\u4e9b\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h3>1. \u8c03\u7528nslookup<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def nslookup(domain):<\/p>\n<p>    try:<\/p>\n<p>        output = subprocess.check_output([&quot;nslookup&quot;, domain], universal_newlines=True)<\/p>\n<p>        print(output)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;nslookup failed: {e}&quot;)<\/p>\n<p>nslookup(&quot;www.example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8c03\u7528dig<\/h3>\n<\/p>\n<p><p><code>dig<\/code>\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684DNS\u67e5\u8be2\u5de5\u5177\uff0c\u4ee5\u4e0b\u662f\u8c03\u7528dig\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dig(domain):<\/p>\n<p>    try:<\/p>\n<p>        output = subprocess.check_output([&quot;dig&quot;, domain, &quot;+short&quot;], universal_newlines=True)<\/p>\n<p>        print(output)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;dig failed: {e}&quot;)<\/p>\n<p>dig(&quot;www.example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6DNS\u4fe1\u606f\u3002<strong>socket\u5e93<\/strong>\u9002\u5408\u7b80\u5355\u7684DNS\u67e5\u8be2\u64cd\u4f5c\uff0c<strong>dnspython\u5e93<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u67e5\u8be2\u7684\u573a\u666f\uff0c\u800c<strong>subprocess\u6a21\u5757<\/strong>\u5219\u53ef\u4ee5\u8c03\u7528\u7cfb\u7edf\u5de5\u5177\u8fdb\u884cDNS\u67e5\u8be2\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u83b7\u53d6DNS\u4fe1\u606f\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u5c06\u6781\u5927\u5730\u63d0\u9ad8\u5728\u7f51\u7edc\u7f16\u7a0b\u548c\u7cfb\u7edf\u7ba1\u7406\u4e2d\u7684\u6548\u7387\u548c\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u7279\u5b9a\u57df\u540d\u7684DNS\u8bb0\u5f55\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u7279\u5b9a\u57df\u540d\u7684DNS\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>dnspython<\/code>\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install dnspython<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u83b7\u53d6DNS\u8bb0\u5f55\uff1a  <\/p>\n<pre><code class=\"language-python\">import dns.resolver\n\ndomain = &#39;example.com&#39;  # \u66ff\u6362\u4e3a\u4f60\u60f3\u67e5\u8be2\u7684\u57df\u540d\nresult = dns.resolver.resolve(domain, &#39;A&#39;)  # \u67e5\u8be2A\u8bb0\u5f55\nfor ipval in result:\n    print(&#39;IP\u5730\u5740:&#39;, ipval.to_text())\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230\u6307\u5b9a\u57df\u540d\u7684A\u8bb0\u5f55\u3002<\/p>\n<p><strong>Python\u4e2d\u83b7\u53d6DNS\u4fe1\u606f\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u83b7\u53d6DNS\u4fe1\u606f\u5e38\u7528\u7684\u5e93\u6709<code>dnspython<\/code>\u3001<code>socket<\/code>\u548c<code>pywhois<\/code>\u7b49\u3002<code>dnspython<\/code>\u4e13\u95e8\u7528\u4e8eDNS\u67e5\u8be2\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u3002<code>socket<\/code>\u5e93\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u4e3b\u673a\u7684IP\u5730\u5740\uff0c\u9002\u5408\u7b80\u5355\u7684DNS\u67e5\u8be2\u3002\u800c<code>pywhois<\/code>\u5219\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u57df\u540d\u7684\u6ce8\u518c\u4fe1\u606f\uff0c\u5305\u62ecDNS\u670d\u52a1\u5668\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2dDNS\u67e5\u8be2\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884cDNS\u67e5\u8be2\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u57df\u540d\u4e0d\u5b58\u5728\u6216\u8005\u7f51\u7edc\u8fde\u63a5\u5931\u8d25\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import dns.resolver\n\ntry:\n    result = dns.resolver.resolve(&#39;example.com&#39;, &#39;A&#39;)\n    for ipval in result:\n        print(&#39;IP\u5730\u5740:&#39;, ipval.to_text())\nexcept dns.resolver.NoAnswer:\n    print(&quot;\u6ca1\u6709\u627e\u5230\u7b54\u6848&quot;)\nexcept dns.resolver.NXDOMAIN:\n    print(&quot;\u57df\u540d\u4e0d\u5b58\u5728&quot;)\nexcept Exception as e:\n    print(&quot;\u53d1\u751f\u9519\u8bef:&quot;, e)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u5728\u9047\u5230\u95ee\u9898\u65f6\u80fd\u591f\u7ed9\u51fa\u53cb\u597d\u7684\u63d0\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u5f00\u5934\u6bb5\u843d \u8981\u5728Python\u4e2d\u83b7\u53d6DNS\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528socket\u5e93\u3001dnspython\u5e93\u3001subproce [&hellip;]","protected":false},"author":3,"featured_media":965071,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965067"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=965067"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965067\/revisions"}],"predecessor-version":[{"id":965074,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965067\/revisions\/965074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/965071"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=965067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=965067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=965067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}