{"id":6210,"date":"2014-04-02T23:55:51","date_gmt":"2014-04-02T15:55:51","guid":{"rendered":"http:\/\/jpuyy.com\/?p=6210"},"modified":"2015-01-15T14:22:41","modified_gmt":"2015-01-15T06:22:41","slug":"python-ip2long-long2ip","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=6210","title":{"rendered":"ip2long\u4e0elong2ip"},"content":{"rendered":"<p>\u5728php\u4e2d\u53ef\u4ee5\u77e5\u9053 ip2long \u51fd\u6570\u5c06ip\u8f6c\u4e3a\u6574\u578b\uff0clong2ip\u5c06\u6574\u578b\u8f6c\u4e3aip\uff0c\u4ece\u800c\u65b9\u4fbf\u8ba1\u7b97\u3002<\/p>\n<p>ip\u5206\u4e3a\u56db\u6bb5\uff0c\u5c06\u7b2c\u4e00\u6bb5*256^3\uff0c\u7b2c\u4e8c\u6bb5*256^2\uff0c\u7b2c\u4e09\u6bb5*256^1\uff0c\u7b2c\u56db\u6bb5*256^0\uff0c\u6700\u540e\u76f8\u52a0\uff0c\u8fd9\u6837\u5c31\u8ba1\u7b97\u51fa\u4e86\u4e00\u4e2a\u552f\u4e00\u7684\u503c\u3002\u5982\u679c\u90fd\u662f\u6700\u5927\u503c\uff0c\u7b97\u51fa\u6765\u7684\u503c\u662f<\/p>\n<p>&gt;&gt;&gt; 255*256*256*256 + 255*256*256 + 255*256 + 255<br \/>\n4294967295<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0mysql\u4e2dint\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f4\u4e2a\u5b57\u8282\uff0c\u5341\u8fdb\u5236\u4e3a<\/p>\n<p>0 \u00a0\u5230 \u00a04294967295<\/p>\n<p>\u6240\u4ee5\u6240\u6709\u7684ipv4\u5730\u5740\u7528mysql\u7684int\u6070\u597d\u53ef\u4ee5\u5b8c\u6574\u8bb0\u5f55<\/p>\n<p>python\u4e2d\u4e92\u76f8\u8f6c\u6362\u7684\u65b9\u6cd5\u5982\u4e0b<\/p>\n<pre>\r\nimport socket \r\nimport struct\r\n\r\ndef ip2long(ipstr): \r\n    return struct.unpack(\"!I\", socket.inet_aton(ipstr))[0]\r\n\r\ndef long2ip(ip): \r\n    return socket.inet_ntoa(struct.pack(\"!I\", ip))\r\n<\/pre>\n<p>http:\/\/hily.me\/blog\/2009\/03\/python-ip2long-long2ip\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728php\u4e2d\u53ef\u4ee5\u77e5\u9053 ip2long \u51fd\u6570\u5c06ip\u8f6c\u4e3a\u6574\u578b\uff0clong2ip\u5c06\u6574\u578b\u8f6c\u4e3aip\uff0c\u4ece\u800c\u65b9\u4fbf\u8ba1\u7b97\u3002 ip\u5206\u4e3a\u56db\u6bb5\uff0c\u5c06\u7b2c\u4e00\u6bb5*256^3\uff0c\u7b2c\u4e8c\u6bb5*256^2\uff0c\u7b2c\u4e09\u6bb5*256^1\uff0c\u7b2c\u56db\u6bb5*256^0\uff0c\u6700\u540e\u76f8\u52a0\uff0c\u8fd9\u6837\u5c31\u8ba1\u7b97\u51fa\u4e86\u4e00\u4e2a\u552f\u4e00\u7684\u503c\u3002\u5982\u679c\u90fd\u662f\u6700\u5927\u503c\uff0c\u7b97\u51fa\u6765\u7684\u503c\u662f &gt;&gt;&gt; 255*256*256*256 + 255*256*256 + 255*256 + 255 4294967295 \u6211\u4eec\u53ef\u4ee5\u53d1\u73b0mysql\u4e2dint\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f4\u4e2a\u5b57\u8282\uff0c\u5341\u8fdb\u5236\u4e3a 0 \u00a0\u5230 \u00a04294967295 \u6240\u4ee5\u6240\u6709\u7684ipv4\u5730\u5740\u7528mysql\u7684int\u6070\u597d\u53ef\u4ee5\u5b8c\u6574\u8bb0\u5f55 python\u4e2d\u4e92\u76f8\u8f6c\u6362\u7684\u65b9\u6cd5\u5982\u4e0b import socket import struct def ip2long(ipstr): return struct.unpack(&#8220;!I&#8221;, socket.inet_aton(ipstr))[0] def long2ip(ip): return socket.inet_ntoa(struct.pack(&#8220;!I&#8221;, ip)) http:\/\/hily.me\/blog\/2009\/03\/python-ip2long-long2ip\/<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-6210","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6210"}],"version-history":[{"count":8,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6210\/revisions"}],"predecessor-version":[{"id":7070,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6210\/revisions\/7070"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}