{"id":1169747,"date":"2025-01-15T16:14:11","date_gmt":"2025-01-15T08:14:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169747.html"},"modified":"2025-01-15T16:14:15","modified_gmt":"2025-01-15T08:14:15","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%adipv4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169747.html","title":{"rendered":"python\u5982\u4f55\u5224\u65adipv4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070633\/73fd47bc-4d33-4086-8a86-ef4e045d569a.webp\" alt=\"python\u5982\u4f55\u5224\u65adipv4\" \/><\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u6a21\u5757 <code>ipaddress<\/code><\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528 Python \u7684\u5185\u7f6e\u6a21\u5757 <code>ipaddress<\/code> \u5224\u65ad IPv4 \u5730\u5740\u7684\u6709\u6548\u6027\u3001\u662f\u9a8c\u8bc1 IPv4 \u5730\u5740\u7684\u6700\u4f73\u65b9\u5f0f\u4e4b\u4e00\u3002<\/strong><\/p>\n<\/p>\n<p><p><code>ipaddress<\/code> \u6a21\u5757\u81ea Python 3.3 \u8d77\u5f15\u5165\uff0c\u63d0\u4f9b\u4e86\u5904\u7406 IPv4 \u548c IPv6 \u5730\u5740\u7684\u529f\u80fd\u3002\u8981\u9a8c\u8bc1\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ipaddress.IPv4Address<\/code> \u7c7b\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u5c06\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import ipaddress<\/p>\n<p>def is_valid_ipv4(ip):<\/p>\n<p>    try:<\/p>\n<p>        ipaddress.IPv4Address(ip)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>ipaddress.IPv4Address(ip)<\/code><\/strong>\uff1a\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32 <code>ip<\/code> \u8f6c\u6362\u4e3a IPv4 \u5730\u5740\u5bf9\u8c61\u3002<\/li>\n<li><strong><code>try<\/code> \u548c <code>except<\/code> \u5757<\/strong>\uff1a\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219 <code>ip<\/code> \u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\uff0c\u5219 <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d IPv4 \u5730\u5740\u3002IPv4 \u5730\u5740\u7531\u56db\u90e8\u5206\u7ec4\u6210\uff0c\u6bcf\u90e8\u5206\u662f 0 \u5230 255 \u4e4b\u95f4\u7684\u4e00\u4e2a\u6570\uff0c\u7528\u70b9\u53f7\u5206\u9694\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9a8c\u8bc1\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_ipv4(ip):<\/p>\n<p>    pattern = re.compile(r&#39;^(\\d{1,3}\\.){3}\\d{1,3}$&#39;)<\/p>\n<p>    if pattern.match(ip):<\/p>\n<p>        parts = ip.split(&#39;.&#39;)<\/p>\n<p>        for part in parts:<\/p>\n<p>            if int(part) &lt; 0 or int(part) &gt; 255:<\/p>\n<p>                return False<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>re.compile(r&#39;^(\\d{1,3}\\.){3}\\d{1,3}\uff1a\u7f16\u8bd1\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5339\u914d\u683c\u5f0f\u4e3a <\/code>x.x.x.x<code>\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d<\/code>x` \u662f 1 \u5230 3 \u4f4d\u7684\u6570\u5b57\u3002<\/li>\n<li><strong><code>pattern.match(ip)<\/code><\/strong>\uff1a\u68c0\u67e5\u5b57\u7b26\u4e32 <code>ip<\/code> \u662f\u5426\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f\u3002<\/li>\n<li><strong><code>ip.split(&#39;.&#39;)<\/code><\/strong>\uff1a\u5c06 <code>ip<\/code> \u6309\u70b9\u53f7\u5206\u5272\u6210\u56db\u90e8\u5206\u3002<\/li>\n<li><strong><code>int(part) &lt; 0 or int(part) &gt; 255<\/code><\/strong>\uff1a\u68c0\u67e5\u6bcf\u90e8\u5206\u662f\u5426\u5728 0 \u5230 255 \u4e4b\u95f4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u60a8\u4e5f\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u901a\u8fc7\u62c6\u5206\u5b57\u7b26\u4e32\u5e76\u9010\u4e00\u68c0\u67e5\u5404\u90e8\u5206\u7684\u6709\u6548\u6027\u6765\u9a8c\u8bc1 IPv4 \u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid_ipv4(ip):<\/p>\n<p>    parts = ip.split(&#39;.&#39;)<\/p>\n<p>    if len(parts) != 4:<\/p>\n<p>        return False<\/p>\n<p>    for part in parts:<\/p>\n<p>        if not part.isdigit():<\/p>\n<p>            return False<\/p>\n<p>        if int(part) &lt; 0 or int(part) &gt; 255:<\/p>\n<p>            return False<\/p>\n<p>    return True<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>ip.split(&#39;.&#39;)<\/code><\/strong>\uff1a\u5c06 <code>ip<\/code> \u6309\u70b9\u53f7\u5206\u5272\u6210\u56db\u90e8\u5206\u3002<\/li>\n<li><strong><code>len(parts) != 4<\/code><\/strong>\uff1a\u68c0\u67e5\u662f\u5426\u5206\u5272\u6210\u4e86\u56db\u90e8\u5206\u3002<\/li>\n<li><strong><code>part.isdigit()<\/code><\/strong>\uff1a\u68c0\u67e5\u6bcf\u90e8\u5206\u662f\u5426\u5168\u90e8\u7531\u6570\u5b57\u7ec4\u6210\u3002<\/li>\n<li><strong><code>int(part) &lt; 0 or int(part) &gt; 255<\/code><\/strong>\uff1a\u68c0\u67e5\u6bcf\u90e8\u5206\u662f\u5426\u5728 0 \u5230 255 \u4e4b\u95f4\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5916\u90e8\u5e93 <code>netaddr<\/code><\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>ipaddress<\/code>\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5916\u90e8\u5e93 <code>netaddr<\/code> \u6765\u9a8c\u8bc1 IPv4 \u5730\u5740\u3002<code>netaddr<\/code> \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7f51\u7edc\u5730\u5740\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406 IP \u5730\u5740\u548c\u7f51\u7edc\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5 <code>netaddr<\/code>\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install netaddr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from netaddr import IPAddress, AddrFormatError<\/p>\n<p>def is_valid_ipv4(ip):<\/p>\n<p>    try:<\/p>\n<p>        IPAddress(ip, version=4)<\/p>\n<p>        return True<\/p>\n<p>    except AddrFormatError:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>IPAddress(ip, version=4)<\/code><\/strong>\uff1a\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32 <code>ip<\/code> \u8f6c\u6362\u4e3a IPv4 \u5730\u5740\u5bf9\u8c61\u3002\u5982\u679c <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u5c06\u5f15\u53d1 <code>AddrFormatError<\/code> \u5f02\u5e38\u3002<\/li>\n<li><strong><code>try<\/code> \u548c <code>except<\/code> \u5757<\/strong>\uff1a\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219 <code>ip<\/code> \u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u5f15\u53d1 <code>AddrFormatError<\/code> \u5f02\u5e38\uff0c\u5219 <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>socket<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>socket<\/code> \u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1 IPv4 \u5730\u5740\u3002<code>socket<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u7f51\u7edc\u901a\u4fe1\u7684\u5e95\u5c42\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a socket \u6765\u9a8c\u8bc1 IP \u5730\u5740\u7684\u6709\u6548\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def is_valid_ipv4(ip):<\/p>\n<p>    try:<\/p>\n<p>        socket.inet_pton(socket.AF_INET, ip)<\/p>\n<p>        return True<\/p>\n<p>    except socket.error:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>socket.inet_pton(socket.AF_INET, ip)<\/code><\/strong>\uff1a\u5c06\u5b57\u7b26\u4e32 <code>ip<\/code> \u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u3002\u5982\u679c <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u5c06\u5f15\u53d1 <code>socket.error<\/code> \u5f02\u5e38\u3002<\/li>\n<li><strong><code>try<\/code> \u548c <code>except<\/code> \u5757<\/strong>\uff1a\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219 <code>ip<\/code> \u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u5f15\u53d1 <code>socket.error<\/code> \u5f02\u5e38\uff0c\u5219 <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5224\u65ad IPv4 \u5730\u5740\u662f\u5426\u6709\u6548\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u7684\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong><code>ipaddress<\/code> \u6a21\u5757<\/strong>\uff1a\u4f7f\u7528 Python \u5185\u7f6e\u7684 <code>ipaddress<\/code> \u6a21\u5757\u662f\u6700\u7b80\u4fbf\u548c\u53ef\u9760\u7684\u65b9\u6cd5\u3002\u9002\u7528\u4e8e Python 3.3 \u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u903b\u8f91\uff0c\u4f46\u4ee3\u7801\u8f83\u590d\u6742\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\uff1a\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u9a8c\u8bc1 IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u5b8c\u5168\u63a7\u5236\u9a8c\u8bc1\u8fc7\u7a0b\uff0c\u4f46\u9700\u8981\u624b\u52a8\u5904\u7406\u5404\u79cd\u8fb9\u754c\u60c5\u51b5\u3002<\/li>\n<li><strong><code>netaddr<\/code> \u5e93<\/strong>\uff1a\u4f7f\u7528\u5916\u90e8\u5e93 <code>netaddr<\/code> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5\u4f9d\u8d56\u3002<\/li>\n<li><strong><code>socket<\/code> \u6a21\u5757<\/strong>\uff1a\u4f7f\u7528 <code>socket<\/code> \u6a21\u5757\u9a8c\u8bc1 IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u5229\u7528\u5e95\u5c42\u7f51\u7edc\u63a5\u53e3\uff0c\u4f46\u4ee3\u7801\u8f83\u4e3a\u5e95\u5c42\u3002<\/li>\n<\/ol>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u8003\u8651\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4f7f\u7528 <code>ipaddress<\/code> \u6a21\u5757\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5e76\u4e14\u6613\u4e8e\u4f7f\u7528\u3002)<code>&lt;\/strong&gt;\uff1a\u7f16\u8bd1\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5339\u914d\u683c\u5f0f\u4e3a <\/code>x.x.x.x<code>\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d<\/code>x` \u662f 1 \u5230 3 \u4f4d\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<ul>\n<li><strong><code>pattern.match(ip)<\/code><\/strong>\uff1a\u68c0\u67e5\u5b57\u7b26\u4e32 <code>ip<\/code> \u662f\u5426\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f\u3002<\/li>\n<li><strong><code>ip.split(&#39;.&#39;)<\/code><\/strong>\uff1a\u5c06 <code>ip<\/code> \u6309\u70b9\u53f7\u5206\u5272\u6210\u56db\u90e8\u5206\u3002<\/li>\n<li><strong><code>int(part) &lt; 0 or int(part) &gt; 255<\/code><\/strong>\uff1a\u68c0\u67e5\u6bcf\u90e8\u5206\u662f\u5426\u5728 0 \u5230 255 \u4e4b\u95f4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u60a8\u4e5f\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u901a\u8fc7\u62c6\u5206\u5b57\u7b26\u4e32\u5e76\u9010\u4e00\u68c0\u67e5\u5404\u90e8\u5206\u7684\u6709\u6548\u6027\u6765\u9a8c\u8bc1 IPv4 \u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid_ipv4(ip):<\/p>\n<p>    parts = ip.split(&#39;.&#39;)<\/p>\n<p>    if len(parts) != 4:<\/p>\n<p>        return False<\/p>\n<p>    for part in parts:<\/p>\n<p>        if not part.isdigit():<\/p>\n<p>            return False<\/p>\n<p>        if int(part) &lt; 0 or int(part) &gt; 255:<\/p>\n<p>            return False<\/p>\n<p>    return True<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>ip.split(&#39;.&#39;)<\/code><\/strong>\uff1a\u5c06 <code>ip<\/code> \u6309\u70b9\u53f7\u5206\u5272\u6210\u56db\u90e8\u5206\u3002<\/li>\n<li><strong><code>len(parts) != 4<\/code><\/strong>\uff1a\u68c0\u67e5\u662f\u5426\u5206\u5272\u6210\u4e86\u56db\u90e8\u5206\u3002<\/li>\n<li><strong><code>part.isdigit()<\/code><\/strong>\uff1a\u68c0\u67e5\u6bcf\u90e8\u5206\u662f\u5426\u5168\u90e8\u7531\u6570\u5b57\u7ec4\u6210\u3002<\/li>\n<li><strong><code>int(part) &lt; 0 or int(part) &gt; 255<\/code><\/strong>\uff1a\u68c0\u67e5\u6bcf\u90e8\u5206\u662f\u5426\u5728 0 \u5230 255 \u4e4b\u95f4\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5916\u90e8\u5e93 <code>netaddr<\/code><\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>ipaddress<\/code>\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5916\u90e8\u5e93 <code>netaddr<\/code> \u6765\u9a8c\u8bc1 IPv4 \u5730\u5740\u3002<code>netaddr<\/code> \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7f51\u7edc\u5730\u5740\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406 IP \u5730\u5740\u548c\u7f51\u7edc\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5 <code>netaddr<\/code>\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install netaddr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from netaddr import IPAddress, AddrFormatError<\/p>\n<p>def is_valid_ipv4(ip):<\/p>\n<p>    try:<\/p>\n<p>        IPAddress(ip, version=4)<\/p>\n<p>        return True<\/p>\n<p>    except AddrFormatError:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>IPAddress(ip, version=4)<\/code><\/strong>\uff1a\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32 <code>ip<\/code> \u8f6c\u6362\u4e3a IPv4 \u5730\u5740\u5bf9\u8c61\u3002\u5982\u679c <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u5c06\u5f15\u53d1 <code>AddrFormatError<\/code> \u5f02\u5e38\u3002<\/li>\n<li><strong><code>try<\/code> \u548c <code>except<\/code> \u5757<\/strong>\uff1a\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219 <code>ip<\/code> \u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u5f15\u53d1 <code>AddrFormatError<\/code> \u5f02\u5e38\uff0c\u5219 <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>socket<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>socket<\/code> \u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1 IPv4 \u5730\u5740\u3002<code>socket<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u7f51\u7edc\u901a\u4fe1\u7684\u5e95\u5c42\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a socket \u6765\u9a8c\u8bc1 IP \u5730\u5740\u7684\u6709\u6548\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def is_valid_ipv4(ip):<\/p>\n<p>    try:<\/p>\n<p>        socket.inet_pton(socket.AF_INET, ip)<\/p>\n<p>        return True<\/p>\n<p>    except socket.error:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>print(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True<\/p>\n<p>print(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong><code>socket.inet_pton(socket.AF_INET, ip)<\/code><\/strong>\uff1a\u5c06\u5b57\u7b26\u4e32 <code>ip<\/code> \u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u3002\u5982\u679c <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u5c06\u5f15\u53d1 <code>socket.error<\/code> \u5f02\u5e38\u3002<\/li>\n<li><strong><code>try<\/code> \u548c <code>except<\/code> \u5757<\/strong>\uff1a\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219 <code>ip<\/code> \u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u5f15\u53d1 <code>socket.error<\/code> \u5f02\u5e38\uff0c\u5219 <code>ip<\/code> \u4e0d\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5224\u65ad IPv4 \u5730\u5740\u662f\u5426\u6709\u6548\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u7684\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong><code>ipaddress<\/code> \u6a21\u5757<\/strong>\uff1a\u4f7f\u7528 Python \u5185\u7f6e\u7684 <code>ipaddress<\/code> \u6a21\u5757\u662f\u6700\u7b80\u4fbf\u548c\u53ef\u9760\u7684\u65b9\u6cd5\u3002\u9002\u7528\u4e8e Python 3.3 \u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u903b\u8f91\uff0c\u4f46\u4ee3\u7801\u8f83\u590d\u6742\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\uff1a\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u9a8c\u8bc1 IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u5b8c\u5168\u63a7\u5236\u9a8c\u8bc1\u8fc7\u7a0b\uff0c\u4f46\u9700\u8981\u624b\u52a8\u5904\u7406\u5404\u79cd\u8fb9\u754c\u60c5\u51b5\u3002<\/li>\n<li><strong><code>netaddr<\/code> \u5e93<\/strong>\uff1a\u4f7f\u7528\u5916\u90e8\u5e93 <code>netaddr<\/code> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5\u4f9d\u8d56\u3002<\/li>\n<li><strong><code>socket<\/code> \u6a21\u5757<\/strong>\uff1a\u4f7f\u7528 <code>socket<\/code> \u6a21\u5757\u9a8c\u8bc1 IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u5229\u7528\u5e95\u5c42\u7f51\u7edc\u63a5\u53e3\uff0c\u4f46\u4ee3\u7801\u8f83\u4e3a\u5e95\u5c42\u3002<\/li>\n<\/ol>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u8003\u8651\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4f7f\u7528 <code>ipaddress<\/code> \u6a21\u5757\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5e76\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9a8c\u8bc1\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u662f\u6709\u6548\u7684IPv4\u5730\u5740\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>ipaddress<\/code>\u6a21\u5757\u6765\u9a8c\u8bc1IPv4\u5730\u5740\u3002\u901a\u8fc7<code>ipaddress.IPv4Address<\/code>\u7c7b\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aIPv4\u5730\u5740\uff0c\u5982\u679c\u6210\u529f\uff0c\u5219\u8be5\u5b57\u7b26\u4e32\u662f\u6709\u6548\u7684IPv4\u5730\u5740\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import ipaddress\n\ndef is_valid_ipv4(address):\n    try:\n        ip = ipaddress.IPv4Address(address)\n        return True\n    except ipaddress.AddressValueError:\n        return False\n\nprint(is_valid_ipv4(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True\nprint(is_valid_ipv4(&quot;256.256.256.256&quot;))  # \u8f93\u51fa: False\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406IPv4\u5730\u5740\u7684\u683c\u5f0f\u5316\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5904\u7406IPv4\u5730\u5740\u65f6\uff0c\u786e\u4fdd\u5730\u5740\u7684\u683c\u5f0f\u662f\u6b63\u786e\u7684\uff0c\u5305\u62ec\u56db\u4e2a\u6570\u5b57\u6bb5\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u8303\u56f4\u57280\u5230255\u4e4b\u95f4\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u683c\u5f0f\u5316\u68c0\u67e5\uff0c\u786e\u4fdd\u8f93\u5165\u7684IPv4\u5730\u5740\u7b26\u5408\u6807\u51c6\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ndef is_proper_ipv4_format(address):\n    pattern = r&#39;^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$&#39;\n    return re.match(pattern, address) is not None\n\nprint(is_proper_ipv4_format(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: True\nprint(is_proper_ipv4_format(&quot;192.168.1.300&quot;))  # \u8f93\u51fa: False\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u6709\u6548\u7684IPv4\u5730\u5740\u8f6c\u6362\u4e3a\u6574\u6570\u5f62\u5f0f\uff1f<\/strong><br \/>IPv4\u5730\u5740\u53ef\u4ee5\u901a\u8fc7<code>ipaddress<\/code>\u6a21\u5757\u8f7b\u677e\u8f6c\u6362\u4e3a\u6574\u6570\u5f62\u5f0f\u3002\u8fd9\u5bf9\u4e8e\u67d0\u4e9b\u7f51\u7edc\u7f16\u7a0b\u5e94\u7528\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6bd4\u8f83\u548c\u5b58\u50a8\u3002\u4f7f\u7528<code>ipaddress.IPv4Address<\/code>\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import ipaddress\n\ndef ipv4_to_integer(address):\n    ip = ipaddress.IPv4Address(address)\n    return int(ip)\n\nprint(ipv4_to_integer(&quot;192.168.1.1&quot;))  # \u8f93\u51fa: 3232235777\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u5224\u65ad\u548c\u5904\u7406IPv4\u5730\u5740\uff0c\u786e\u4fdd\u7f51\u7edc\u7f16\u7a0b\u7684\u51c6\u786e\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u6a21\u5757 ipaddress \u4f7f\u7528 Python \u7684\u5185\u7f6e\u6a21\u5757 ipaddress \u5224\u65ad IPv4 \u5730\u5740 [&hellip;]","protected":false},"author":3,"featured_media":1169750,"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\/1169747"}],"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=1169747"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169747\/revisions"}],"predecessor-version":[{"id":1169751,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169747\/revisions\/1169751"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169750"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}