{"id":998395,"date":"2024-12-27T09:32:59","date_gmt":"2024-12-27T01:32:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998395.html"},"modified":"2024-12-27T09:33:01","modified_gmt":"2024-12-27T01:33:01","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e5%ae%9aip%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998395.html","title":{"rendered":"python\u5982\u4f55\u5224\u5b9aip\u5730\u5740"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073839\/1d48133a-a72d-45d4-b055-21a83704fda0.webp\" alt=\"python\u5982\u4f55\u5224\u5b9aip\u5730\u5740\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u5b9aIP\u5730\u5740\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u5e93<code>ipaddress<\/code>\u8fdb\u884c\u89e3\u6790\u548c\u9a8c\u8bc1\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u683c\u5f0f\u5339\u914d\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>ipaddress<\/code>\u5e93\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7b80\u6d01\u7684\u63a5\u53e3\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u5904\u7406\u548c\u9a8c\u8bc1IPv4\u548cIPv6\u5730\u5740\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>ipaddress<\/code>\u5e93\u9a8c\u8bc1IP\u5730\u5740<\/p>\n<\/p>\n<p><p><code>ipaddress<\/code>\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u9a8c\u8bc1IP\u5730\u5740\u53ca\u7f51\u7edc\u3002\u8be5\u5e93\u81eaPython 3.3\u5f00\u59cb\u88ab\u5f15\u5165\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u6765\u68c0\u67e5IP\u5730\u5740\u7684\u6709\u6548\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9a8c\u8bc1IPv4\u548cIPv6\u5730\u5740<\/strong><\/p>\n<\/p>\n<p><p><code>ipaddress<\/code>\u5e93\u53ef\u4ee5\u901a\u8fc7<code>ip_address()<\/code>\u51fd\u6570\u6765\u9a8c\u8bc1IP\u5730\u5740\u7684\u683c\u5f0f\uff0c\u5e76\u5224\u65ad\u5176\u662fIPv4\u8fd8\u662fIPv6\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ipaddress<\/p>\n<p>def is_valid_ip(ip):<\/p>\n<p>    try:<\/p>\n<p>        ip_obj = ipaddress.ip_address(ip)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(is_valid_ip(&#39;192.168.0.1&#39;))  # True<\/p>\n<p>print(is_valid_ip(&#39;256.256.256.256&#39;))  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>ip_address()<\/code>\u51fd\u6570\u4f1a\u5c1d\u8bd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u4e00\u4e2aIP\u5730\u5740\u5bf9\u8c61\u3002\u5982\u679c\u6210\u529f\uff0c\u8bf4\u660e\u8f93\u5165\u662f\u4e00\u4e2a\u6709\u6548\u7684IP\u5730\u5740\uff1b\u5982\u679c\u629b\u51fa<code>ValueError<\/code>\u5f02\u5e38\uff0c\u5219\u8bf4\u660e\u8f93\u5165\u4e0d\u662f\u6709\u6548\u7684IP\u5730\u5740\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5224\u65adIP\u5730\u5740\u7684\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>ipaddress<\/code>\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2aIP\u5730\u5740\u662fIPv4\u8fd8\u662fIPv6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_ip_type(ip):<\/p>\n<p>    try:<\/p>\n<p>        ip_obj = ipaddress.ip_address(ip)<\/p>\n<p>        if isinstance(ip_obj, ipaddress.IPv4Address):<\/p>\n<p>            return &#39;IPv4&#39;<\/p>\n<p>        elif isinstance(ip_obj, ipaddress.IPv6Address):<\/p>\n<p>            return &#39;IPv6&#39;<\/p>\n<p>    except ValueError:<\/p>\n<p>        return &#39;Invalid IP&#39;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(get_ip_type(&#39;192.168.0.1&#39;))  # IPv4<\/p>\n<p>print(get_ip_type(&#39;::1&#39;))  # IPv6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>isinstance()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u751f\u6210\u7684IP\u5730\u5740\u5bf9\u8c61\u5c5e\u4e8e<code>IPv4Address<\/code>\u7c7b\u8fd8\u662f<code>IPv6Address<\/code>\u7c7b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dIP\u5730\u5740<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>ipaddress<\/code>\u5e93\u5df2\u7ecf\u80fd\u591f\u5f88\u597d\u5730\u5904\u7406IP\u5730\u5740\u7684\u89e3\u6790\u548c\u9a8c\u8bc1\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5bf9IP\u5730\u5740\u683c\u5f0f\u8fdb\u884c\u7b80\u5355\u7684\u5339\u914d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5339\u914dIPv4\u5730\u5740<\/strong><\/p>\n<\/p>\n<p><p>IPv4\u5730\u5740\u7531\u56db\u4e2a\u4ee5\u70b9\u5206\u9694\u7684\u5341\u8fdb\u5236\u6570\u6784\u6210\uff0c\u6bcf\u4e2a\u6570\u503c\u7684\u8303\u56f4\u662f0\u5230255\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u8fd9\u79cd\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_ipv4(ip):<\/p>\n<p>    pattern = r&#39;^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$&#39;<\/p>\n<p>    return re.match(pattern, ip) is not None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(is_valid_ipv4(&#39;192.168.0.1&#39;))  # True<\/p>\n<p>print(is_valid_ipv4(&#39;999.999.999.999&#39;))  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u786e\u4fdd\u6bcf\u4e2a\u6bb5\u843d\u7684\u503c\u57280\u5230255\u4e4b\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5339\u914dIPv6\u5730\u5740<\/strong><\/p>\n<\/p>\n<p><p>IPv6\u5730\u5740\u7531\u516b\u7ec4\u4ee5\u5192\u53f7\u5206\u9694\u7684\u5341\u516d\u8fdb\u5236\u6570\u6784\u6210\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u76f8\u5bf9\u590d\u6742\u4e00\u4e9b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid_ipv6(ip):<\/p>\n<p>    pattern = r&#39;(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3,3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3,3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))&#39;<\/p>\n<p>    return re.match(pattern, ip) is not None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(is_valid_ipv6(&#39;2001:0db8:85a3:0000:0000:8a2e:0370:7334&#39;))  # True<\/p>\n<p>print(is_valid_ipv6(&#39;2001:db8::1234:&#39;))  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u8986\u76d6\u4e86IPv6\u5730\u5740\u7684\u591a\u79cd\u5408\u6cd5\u683c\u5f0f\uff0c\u5305\u62ec\u538b\u7f29\u8868\u793a\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7ed3\u5408\u4f7f\u7528<code>ipaddress<\/code>\u5e93\u548c\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408<code>ipaddress<\/code>\u5e93\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u5bf9IP\u5730\u5740\u7684\u66f4\u5168\u9762\u7684\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u9996\u5148\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u521d\u6b65\u683c\u5f0f\u68c0\u67e5\uff0c\u7136\u540e\u4f7f\u7528<code>ipaddress<\/code>\u5e93\u8fdb\u884c\u89e3\u6790\u548c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid_ip_combined(ip):<\/p>\n<p>    ipv4_pattern = r&#39;^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$&#39;<\/p>\n<p>    ipv6_pattern = r&#39;(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3,3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3,3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))&#39;<\/p>\n<p>    if re.match(ipv4_pattern, ip) or re.match(ipv6_pattern, ip):<\/p>\n<p>        return is_valid_ip(ip)<\/p>\n<p>    return False<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(is_valid_ip_combined(&#39;192.168.0.1&#39;))  # True<\/p>\n<p>print(is_valid_ip_combined(&#39;999.999.999.999&#39;))  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u7ed3\u5408\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7075\u6d3b\u6027\u548c<code>ipaddress<\/code>\u5e93\u7684\u4e25\u8c28\u6027\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5904\u7406\u590d\u6742\u7684IP\u5730\u5740\u9a8c\u8bc1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001IP\u5730\u5740\u7684\u5176\u4ed6\u5904\u7406\u9700\u6c42<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u9a8c\u8bc1\u548c\u5224\u65ad\uff0c<code>ipaddress<\/code>\u5e93\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u5904\u7406IP\u5730\u5740\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u8ba1\u7b97\u7f51\u7edc\u5730\u5740\u3001\u5b50\u7f51\u5212\u5206\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8ba1\u7b97\u7f51\u7edc\u5730\u5740<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>ip_network()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2aIP\u5730\u5740\u7684\u7f51\u7edc\u5730\u5740\u548c\u5e7f\u64ad\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_network_info(ip, mask):<\/p>\n<p>    try:<\/p>\n<p>        network = ipaddress.ip_network(f&#39;{ip}\/{mask}&#39;, strict=False)<\/p>\n<p>        return {<\/p>\n<p>            &#39;network_address&#39;: str(network.network_address),<\/p>\n<p>            &#39;broadcast_address&#39;: str(network.broadcast_address)<\/p>\n<p>        }<\/p>\n<p>    except ValueError:<\/p>\n<p>        return &#39;Invalid network&#39;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(get_network_info(&#39;192.168.0.1&#39;, &#39;24&#39;))  # {&#39;network_address&#39;: &#39;192.168.0.0&#39;, &#39;broadcast_address&#39;: &#39;192.168.0.255&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u4e86<code>ip_network()<\/code>\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2aIP\u5730\u5740\u548c\u5b50\u7f51\u63a9\u7801\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<code>IPv4Network<\/code>\u6216<code>IPv6Network<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b50\u7f51\u5212\u5206<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7ba1\u7406\u4e2d\uff0c\u5b50\u7f51\u5212\u5206\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>ipaddress<\/code>\u5e93\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def subnet_division(ip, prefix):<\/p>\n<p>    try:<\/p>\n<p>        network = ipaddress.ip_network(ip)<\/p>\n<p>        subnets = list(network.subnets(new_prefix=prefix))<\/p>\n<p>        return [str(subnet) for subnet in subnets]<\/p>\n<p>    except ValueError:<\/p>\n<p>        return &#39;Invalid network or prefix&#39;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(subnet_division(&#39;192.168.0.0\/24&#39;, 26))  # [&#39;192.168.0.0\/26&#39;, &#39;192.168.0.64\/26&#39;, &#39;192.168.0.128\/26&#39;, &#39;192.168.0.192\/26&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>subnets()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u65b0\u524d\u7f00\u957f\u5ea6\u5c06\u4e00\u4e2a\u7f51\u7edc\u5212\u5206\u4e3a\u66f4\u5c0f\u7684\u5b50\u7f51\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>ipaddress<\/code>\u5e93\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5b9e\u73b0\u5bf9IP\u5730\u5740\u7684\u5404\u79cd\u64cd\u4f5c\u548c\u5904\u7406\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u9ad8\u6548\u800c\u4e14\u7b80\u6d01\uff0c\u975e\u5e38\u9002\u5408\u7f51\u7edc\u7ba1\u7406\u548c\u6570\u636e\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9a8c\u8bc1IP\u5730\u5740\u7684\u683c\u5f0f\u662f\u5426\u6b63\u786e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ipaddress<\/code>\u6a21\u5757\u6765\u9a8c\u8bc1IP\u5730\u5740\u7684\u683c\u5f0f\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u6765\u521b\u5efa\u548c\u68c0\u67e5IP\u5730\u5740\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a<code>IPv4Address<\/code>\u6216<code>IPv6Address<\/code>\u5bf9\u8c61\u6765\u5224\u65adIP\u5730\u5740\u662f\u5426\u6709\u6548\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import ipaddress\n\ndef validate_ip(ip):\n    try:\n        ipaddress.ip_address(ip)\n        return True\n    except ValueError:\n        return False\n\nprint(validate_ip(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True\nprint(validate_ip(&quot;999.999.999.999&quot;))  # \u8f93\u51fa: False\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u533a\u5206IPv4\u548cIPv6\u5730\u5740\uff1f<\/strong><br \/>\u4f7f\u7528<code>ipaddress<\/code>\u6a21\u5757\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u533a\u5206IPv4\u548cIPv6\u5730\u5740\u3002\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a<code>ip_address<\/code>\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad\u5bf9\u8c61\u7684\u7c7b\u578b\u6765\u786e\u5b9a\u5b83\u662fIPv4\u8fd8\u662fIPv6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import ipaddress\n\ndef check_ip_type(ip):\n    try:\n        ip_obj = ipaddress.ip_address(ip)\n        return &quot;IPv4&quot; if isinstance(ip_obj, ipaddress.IPv4Address) else &quot;IPv6&quot;\n    except ValueError:\n        return &quot;\u65e0\u6548\u7684IP\u5730\u5740&quot;\n\nprint(check_ip_type(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: IPv4\nprint(check_ip_type(&quot;::1&quot;))  # \u8f93\u51fa: IPv6\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6IP\u5730\u5740\u7684\u7f51\u7edc\u4fe1\u606f\uff1f<\/strong><br \/><code>ipaddress<\/code>\u6a21\u5757\u4e0d\u4ec5\u53ef\u4ee5\u9a8c\u8bc1\u548c\u533a\u5206IP\u5730\u5740\uff0c\u8fd8\u80fd\u63d0\u4f9b\u7f51\u7edc\u4fe1\u606f\u3002\u901a\u8fc7\u521b\u5efa<code>IPv4Network<\/code>\u6216<code>IPv6Network<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u83b7\u53d6\u7f51\u7edc\u5730\u5740\u3001\u5e7f\u64ad\u5730\u5740\u7b49\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import ipaddress\n\ndef get_network_info(ip, netmask):\n    network = ipaddress.ip_network(f&quot;{ip}\/{netmask}&quot;, strict=False)\n    return {\n        &quot;\u7f51\u7edc\u5730\u5740&quot;: str(network.network_address),\n        &quot;\u5e7f\u64ad\u5730\u5740&quot;: str(network.broadcast_address),\n        &quot;\u4e3b\u673a\u6570\u91cf&quot;: network.num_addresses,\n        &quot;\u53ef\u7528\u4e3b\u673a&quot;: list(network.hosts())\n    }\n\ninfo = get_network_info(&quot;192.168.1.1&quot;, &quot;255.255.255.0&quot;)\nprint(info)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u4ee3\u7801\u6bb5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u6709\u6548\u5730\u5904\u7406\u548c\u5206\u6790IP\u5730\u5740\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u5b9aIP\u5730\u5740\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u5e93ipaddress\u8fdb\u884c\u89e3\u6790\u548c\u9a8c\u8bc1\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u683c\u5f0f\u5339\u914d\u3002 [&hellip;]","protected":false},"author":3,"featured_media":998404,"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\/998395"}],"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=998395"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998395\/revisions"}],"predecessor-version":[{"id":998408,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998395\/revisions\/998408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998404"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}