{"id":986706,"date":"2024-12-27T07:47:19","date_gmt":"2024-12-26T23:47:19","guid":{"rendered":""},"modified":"2024-12-27T07:47:22","modified_gmt":"2024-12-26T23:47:22","slug":"python%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99ip%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986706.html","title":{"rendered":"Python\u5982\u4f55\u7f16\u5199ip\u5730\u5740"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063137\/4e933a90-da4d-45bf-88aa-59fa4dc902bd.webp\" alt=\"Python\u5982\u4f55\u7f16\u5199ip\u5730\u5740\" \/><\/p>\n<p><h3>PYTHON\u7f16\u5199IP\u5730\u5740\u7684\u65b9\u6cd5\u6982\u8ff0<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u7f16\u5199IP\u5730\u5740\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001ipaddress\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93\uff08\u5982 netaddr\uff09\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u9996\u5148\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\uff0c\u5b83\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\u4e32\u6765\u751f\u6210IP\u5730\u5740\u3002\u5176\u6b21\uff0cPython\u81ea\u5e26\u7684ipaddress\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9IPv4\u548cIPv6\u5730\u5740\u8fdb\u884c\u5904\u7406\u548c\u9a8c\u8bc1\u7684\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8fdb\u884cIP\u5730\u5740\u64cd\u4f5c\u7684\u5e94\u7528\u3002\u6700\u540e\uff0c\u7b2c\u4e09\u65b9\u5e93\u5982netaddr\u80fd\u591f\u63d0\u4f9b\u66f4\u9ad8\u7ea7\u548c\u590d\u6742\u7684IP\u5730\u5740\u7ba1\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>ipaddress\u6a21\u5757\u662fPython 3.3\u5f15\u5165\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u7528\u4e8e\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u7ba1\u7406IP\u5730\u5740\u548c\u7f51\u7edc\u7684\u5bf9\u8c61\u3002\u8fd9\u4e2a\u6a21\u5757\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u8fdb\u884cIP\u5730\u5740\u9a8c\u8bc1\u3001\u89e3\u6790\u548c\u8ba1\u7b97\u7684\u5e94\u7528\u3002ipaddress\u6a21\u5757\u652f\u6301IPv4\u548cIPv6\u5730\u5740\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u529f\u80fd\uff0c\u5982\u68c0\u67e5IP\u5730\u5740\u7684\u7c7b\u578b\u3001\u8ba1\u7b97\u7f51\u7edc\u63a9\u7801\u3001\u751f\u6210\u5b50\u7f51\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528ipaddress\u6a21\u5757\u6765\u5904\u7406IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u751f\u6210IP\u5730\u5740<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u751f\u6210IP\u5730\u5740\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5c06IP\u5730\u5740\u7684\u5404\u4e2a\u90e8\u5206\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u62fc\u63a5\u662f\u751f\u6210IP\u5730\u5740\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f60\u53ef\u4ee5\u5c06IP\u5730\u5740\u7684\u5404\u4e2a\u90e8\u5206\u5b58\u50a8\u4e3a\u53d8\u91cf\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u5b8c\u6574\u7684IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octet1 = &quot;192&quot;<\/p>\n<p>octet2 = &quot;168&quot;<\/p>\n<p>octet3 = &quot;1&quot;<\/p>\n<p>octet4 = &quot;1&quot;<\/p>\n<p>ip_address = octet1 + &quot;.&quot; + octet2 + &quot;.&quot; + octet3 + &quot;.&quot; + octet4<\/p>\n<p>print(ip_address)  # \u8f93\u51fa: 192.168.1.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u751f\u6210IP\u5730\u5740\u3002\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6216f-strings\uff08Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528str.format()<\/p>\n<p>ip_address = &quot;{}.{}.{}.{}&quot;.format(octet1, octet2, octet3, octet4)<\/p>\n<p>print(ip_address)  # \u8f93\u51fa: 192.168.1.1<\/p>\n<h2><strong>\u4f7f\u7528f-strings<\/strong><\/h2>\n<p>ip_address = f&quot;{octet1}.{octet2}.{octet3}.{octet4}&quot;<\/p>\n<p>print(ip_address)  # \u8f93\u51fa: 192.168.1.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528ipaddress\u6a21\u5757<\/h3>\n<\/p>\n<p><p>ipaddress\u6a21\u5757\u662f\u5904\u7406IP\u5730\u5740\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86IPv4Address, IPv6Address, IPv4Network, IPv6Network\u7b49\u7c7b\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u7ba1\u7406\u548c\u64cd\u4f5cIP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u521b\u5efaIP\u5730\u5740\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528ipaddress\u6a21\u5757\u521b\u5efaIPv4\u548cIPv6\u5730\u5740\u5bf9\u8c61\u3002\u901a\u8fc7\u8fd9\u4e9b\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u6267\u884c\u5404\u79cdIP\u5730\u5740\u64cd\u4f5c\uff0c\u4f8b\u5982\u9a8c\u8bc1\u3001\u6bd4\u8f83\u548c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ipaddress<\/p>\n<h2><strong>\u521b\u5efaIPv4\u5730\u5740\u5bf9\u8c61<\/strong><\/h2>\n<p>ipv4 = ipaddress.IPv4Address(&#39;192.168.1.1&#39;)<\/p>\n<p>print(ipv4)  # \u8f93\u51fa: 192.168.1.1<\/p>\n<h2><strong>\u521b\u5efaIPv6\u5730\u5740\u5bf9\u8c61<\/strong><\/h2>\n<p>ipv6 = ipaddress.IPv6Address(&#39;2001:0db8::1&#39;)<\/p>\n<p>print(ipv6)  # \u8f93\u51fa: 2001:db8::1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 IP\u5730\u5740\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>ipaddress\u6a21\u5757\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u9a8c\u8bc1IP\u5730\u5740\u7684\u5408\u6cd5\u6027\uff0c\u5e76\u68c0\u67e5\u5176\u662f\u5426\u4e3a\u79c1\u6709\u5730\u5740\u3001\u591a\u64ad\u5730\u5740\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5IPv4\u5730\u5740\u662f\u5426\u4e3a\u79c1\u6709\u5730\u5740<\/p>\n<p>print(ipv4.is_private)  # \u8f93\u51fa: True<\/p>\n<h2><strong>\u68c0\u67e5IPv6\u5730\u5740\u662f\u5426\u4e3a\u5168\u5c40\u5730\u5740<\/strong><\/h2>\n<p>print(ipv6.is_global)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u7f51\u7edc\u548c\u5b50\u7f51\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>ipaddress\u6a21\u5757\u8fd8\u652f\u6301\u7f51\u7edc\u548c\u5b50\u7f51\u7684\u8ba1\u7b97\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u7f51\u7edc\u5bf9\u8c61\uff0c\u8ba1\u7b97\u5b50\u7f51\uff0c\u5e76\u68c0\u67e5IP\u5730\u5740\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7f51\u7edc\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaIPv4\u7f51\u7edc\u5bf9\u8c61<\/p>\n<p>network = ipaddress.IPv4Network(&#39;192.168.1.0\/24&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u5b50\u7f51<\/strong><\/h2>\n<p>for ip in network:<\/p>\n<p>    print(ip)<\/p>\n<h2><strong>\u68c0\u67e5IP\u5730\u5740\u662f\u5426\u5728\u7f51\u7edc\u4e2d<\/strong><\/h2>\n<p>print(ipv4 in network)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93netaddr<\/h3>\n<\/p>\n<p><p>netaddr\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684IP\u5730\u5740\u64cd\u4f5c\u529f\u80fd\u3002\u4e0eipaddress\u6a21\u5757\u76f8\u6bd4\uff0cnetaddr\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u7279\u6027\uff0c\u5982IP\u5730\u5740\u8303\u56f4\u3001CIDR\u5408\u5e76\u7b49\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5netaddr<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528netaddr\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install netaddr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u521b\u5efa\u548c\u7ba1\u7406IP\u5730\u5740<\/h4>\n<\/p>\n<p><p>netaddr\u63d0\u4f9b\u4e86IPNetwork\u548cIPRange\u7c7b\uff0c\u5141\u8bb8\u4f60\u66f4\u7075\u6d3b\u5730\u7ba1\u7406IP\u5730\u5740\u548c\u7f51\u7edc\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from netaddr import IPNetwork, IPRange<\/p>\n<h2><strong>\u521b\u5efaIPNetwork\u5bf9\u8c61<\/strong><\/h2>\n<p>network = IPNetwork(&#39;192.168.1.0\/24&#39;)<\/p>\n<h2><strong>\u521b\u5efaIPRange\u5bf9\u8c61<\/strong><\/h2>\n<p>ip_range = IPRange(&#39;192.168.1.1&#39;, &#39;192.168.1.10&#39;)<\/p>\n<h2><strong>\u68c0\u67e5IP\u5730\u5740\u662f\u5426\u5728\u8303\u56f4\u5185<\/strong><\/h2>\n<p>print(&#39;192.168.1.5&#39; in ip_range)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 IP\u5730\u5740\u5408\u5e76\u4e0eCIDR\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>netaddr\u5141\u8bb8\u4f60\u5408\u5e76IP\u5730\u5740\u8303\u56f4\u5e76\u6267\u884cCIDR\uff08\u65e0\u7c7b\u522b\u57df\u95f4\u8def\u7531\uff09\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from netaddr import cidr_merge<\/p>\n<h2><strong>\u5408\u5e76IP\u5730\u5740\u8303\u56f4<\/strong><\/h2>\n<p>ip_list = [&#39;192.168.1.0\/24&#39;, &#39;192.168.2.0\/24&#39;]<\/p>\n<p>merged = cidr_merge(ip_list)<\/p>\n<p>print(list(merged))  # \u8f93\u51fa: [IPNetwork(&#39;192.168.1.0\/23&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u7528\u6848\u4f8b\uff1aIP\u5730\u5740\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7efc\u5408\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001ipaddress\u6a21\u5757\u548cnetaddr\u5e93\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684IP\u5730\u5740\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u7f51\u7edc\u7ba1\u7406\u548c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u521d\u59cb\u5316IP\u5730\u5740<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u521d\u59cb\u5316\u548c\u7ba1\u7406IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ipaddress<\/p>\n<p>class IPAddressManager:<\/p>\n<p>    def __init__(self, ip):<\/p>\n<p>        try:<\/p>\n<p>            self.ip = ipaddress.ip_address(ip)<\/p>\n<p>        except ValueError:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Invalid IP address&quot;)<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return str(self.ip)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 IP\u5730\u5740\u7c7b\u578b\u68c0\u67e5<\/h4>\n<\/p>\n<p><p>\u6dfb\u52a0\u529f\u80fd\u4ee5\u68c0\u67e5IP\u5730\u5740\u7684\u7c7b\u578b\uff0c\u5e76\u786e\u5b9a\u5176\u662f\u5426\u4e3a\u79c1\u6709\u5730\u5740\u3001\u5168\u5c40\u5730\u5740\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">    def is_private(self):<\/p>\n<p>        return self.ip.is_private<\/p>\n<p>    def is_global(self):<\/p>\n<p>        return self.ip.is_global<\/p>\n<p>    def is_multicast(self):<\/p>\n<p>        return self.ip.is_multicast<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>ip_manager = IPAddressManager(&#39;192.168.1.1&#39;)<\/p>\n<p>print(ip_manager)  # \u8f93\u51fa: 192.168.1.1<\/p>\n<p>print(ip_manager.is_private())  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u7f51\u7edc\u64cd\u4f5c\u4e0e\u5b50\u7f51\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u6269\u5c55\u5de5\u5177\u4ee5\u652f\u6301\u7f51\u7edc\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u5b50\u7f51\uff0c\u5e76\u68c0\u67e5IP\u5730\u5740\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7f51\u7edc\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class NetworkManager:<\/p>\n<p>    def __init__(self, network):<\/p>\n<p>        self.network = ipaddress.ip_network(network)<\/p>\n<p>    def get_all_hosts(self):<\/p>\n<p>        return list(self.network.hosts())<\/p>\n<p>    def is_ip_in_network(self, ip):<\/p>\n<p>        return ipaddress.ip_address(ip) in self.network<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>network_manager = NetworkManager(&#39;192.168.1.0\/24&#39;)<\/p>\n<p>print(network_manager.get_all_hosts())  # \u8f93\u51fa: [192.168.1.1, 192.168.1.2, ...]<\/p>\n<p>print(network_manager.is_ip_in_network(&#39;192.168.1.5&#39;))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u7f16\u5199\u548c\u7ba1\u7406IP\u5730\u5740\uff0c\u4ece\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u5230\u5f3a\u5927\u7684ipaddress\u6a21\u5757\u548cnetaddr\u5e93\u3002<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u4ee5\u5feb\u901f\u751f\u6210IP\u5730\u5740\uff0cipaddress\u6a21\u5757\u9002\u5408\u9700\u8981\u8fdb\u884cIP\u5730\u5740\u9a8c\u8bc1\u548c\u8ba1\u7b97\u7684\u5e94\u7528\uff0c\u800cnetaddr\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684IP\u5730\u5740\u7ba1\u7406\u529f\u80fd\u3002<\/strong> \u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u529f\u80fd\u5f3a\u5927\u7684IP\u5730\u5740\u7ba1\u7406\u5de5\u5177\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u8be6\u7ec6\u65b9\u6cd5\u548c\u5b9e\u7528\u6848\u4f8b\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684IP\u5730\u5740\u7ba1\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9a8c\u8bc1\u4e00\u4e2aIP\u5730\u5740\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ipaddress<\/code>\u6a21\u5757\u6765\u9a8c\u8bc1IP\u5730\u5740\u7684\u6709\u6548\u6027\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86<code>ip_address()<\/code>\u51fd\u6570\uff0c\u60a8\u53ea\u9700\u4f20\u5165\u5f85\u9a8c\u8bc1\u7684IP\u5730\u5740\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5730\u5740\u6709\u6548\u5219\u8fd4\u56de\u4e00\u4e2aIP\u5730\u5740\u5bf9\u8c61\uff0c\u5426\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\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\uff1aTrue\nprint(validate_ip(&quot;999.999.999.999&quot;))  # \u8f93\u51fa\uff1aFalse\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u5c06IP\u5730\u5740\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u4f7f\u7528<code>ipaddress<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5c06IP\u5730\u5740\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8c03\u7528<code>ip_address()<\/code>\u51fd\u6570\u540e\uff0c\u60a8\u53ef\u4ee5\u5229\u7528\u8be5\u5bf9\u8c61\u7684<code>int<\/code>\u5c5e\u6027\u83b7\u53d6\u5176\u6574\u6570\u8868\u793a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import ipaddress\n\nip = ipaddress.ip_address(&quot;192.168.1.1&quot;)\nip_int = int(ip)\nprint(ip_int)  # \u8f93\u51fa\uff1a3232235777\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406IPv4\u548cIPv6\u5730\u5740\uff1f<\/strong><br \/>Python\u7684<code>ipaddress<\/code>\u6a21\u5757\u652f\u6301IPv4\u548cIPv6\u5730\u5740\u7684\u5904\u7406\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>ip_address()<\/code>\u51fd\u6570\u6765\u521b\u5efaIP\u5730\u5740\u5bf9\u8c61\uff0c\u65e0\u8bba\u662fIPv4\u8fd8\u662fIPv6\uff0c\u6a21\u5757\u4f1a\u81ea\u52a8\u8bc6\u522b\u3002\u6839\u636e\u9700\u8981\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u7f51\u7edc\u4fe1\u606f\u3001\u5b50\u7f51\u5212\u5206\u7b49\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import ipaddress\n\nipv4 = ipaddress.ip_address(&quot;192.168.1.1&quot;)\nipv6 = ipaddress.ip_address(&quot;2001:0db8:85a3:0000:0000:8a2e:0370:7334&quot;)\n\nprint(ipv4.is_global)  # \u8f93\u51fa\uff1aFalse\nprint(ipv6.is_global)  # \u8f93\u51fa\uff1aTrue\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"PYTHON\u7f16\u5199IP\u5730\u5740\u7684\u65b9\u6cd5\u6982\u8ff0 \u5728Python\u4e2d\u7f16\u5199IP\u5730\u5740\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001ipaddress\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":986715,"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\/986706"}],"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=986706"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986706\/revisions"}],"predecessor-version":[{"id":986716,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986706\/revisions\/986716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986715"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}