{"id":5019,"date":"2020-10-29T09:03:16","date_gmt":"2020-10-29T01:03:16","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5019"},"modified":"2020-10-29T09:03:16","modified_gmt":"2020-10-29T01:03:16","slug":"ip%e5%90%88%e5%b9%b6%e5%b0%8f%e5%b7%a5%e5%85%b7-python%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5019.html","title":{"rendered":"IP\u5408\u5e76\u5c0f\u5de5\u5177-Python\u7248\u672c"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u6700\u8fd1\u7684\u5de5\u4f5c\u4e2d\u6d89\u53ca\u5230\u4e00\u4e9bIP\u5c5e\u6027\u5206\u6790\u7684\u5185\u5bb9\uff0c\u5728\u6700\u540e\u5206\u6790\u6574\u7406\u7684\u5dee\u4e0d\u591a\u4e86\u4e4b\u540e\uff0c\u9700\u8981\u5c06\u5bf9\u5e94\u7684IP\u6216IP\u6bb5\u5408\u5e76\u518d\u5c55\u5f00\u6210\u5355\u4e2aIP\u7684\u5f62\u5f0f\uff0c\u65b9\u4fbf\u7ed9\u5176\u5b83\u7cfb\u7edf\/\u5de5\u5177\u4f7f\u7528\u3002\u8fd9\u6b21\u662f\u7528Python\u5199\u7684\uff0c\u4e3b\u8981\u662f\u501f\u52a9netaddr\u6a21\u5757\u7684\u529f\u80fd\u6765\u5b9e\u73b0IP\u6bb5\u7684\u5408\u5e76\uff0c\u540e\u7eed\u62bd\u65f6\u95f4\u770b\u80fd\u4e0d\u80fd\u7528Golang\u5b9e\u73b0\u4e00\u7248\uff0c\u4e5f\u5b66\u4e60\u79ef\u7d2f\u4e00\u4e0bGolang\u7684\u7f16\u7a0b\u7ecf\u9a8c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p>\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u653e\u7f6e\u4e00\u4e2a\u540d\u4e3a\u3010IP-company.txt\u3011\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># company_name\nip1 - ip2\nip3\nip4\/28\n\n# company_name2\nipX - ipY\nip11<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6267\u884c\u8be5\u811a\u672c\uff0c\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210\u540d\u4e3a\u3010ip_company.txt\u3011\u7684\u7ed3\u679c\u6587\u4ef6\uff0c\u6587\u4ef6\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><code>ip1,company_name,20201022<br>\u2026<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n# coding=utf-8\nimport sys, time\nfrom netaddr import *\n\ndef ip_format(ip_str):\n    var_list = &#91;]\n    for item in ip_str.strip().split('.'):\n        var_list.append(str(int(item)))\n    return '.'.join(var_list)\n\ndef main():\n    company_ip_map = {}\n    company_str = ''\n    ipset = IPSet()\n    with open('IP-company.txt') as fp:\n        for line in fp:\n            line = line.strip()\n            if line:\n                if line.startswith('#') and len(line) > 1:\n                    company_str = line&#91;1:].lower().strip()\n                    ipset.clear()\n                    continue\n                else:\n                    if ' - ' in line:\n                        ip_start, ip_end = line.split(' - ', 1)\n                        ipset.add(IPRange(ip_format(ip_start), ip_format(ip_end)))\n                    else:\n                        ipset.add(line)\n                    #\n                    if company_str in company_ip_map:\n                        company_ip_map&#91;company_str] = company_ip_map&#91;company_str] | ipset\n                    else:\n                        company_ip_map&#91;company_str] = ipset\n                    #\n                #\n            else:\n                continue\n            #\n        #\n    #\n    for item in company_ip_map:\n        print(item, company_ip_map&#91;item].size)\n    #\n    with open('ip_company.txt', 'w') as fw:\n        for item in company_ip_map:\n            print(item, company_ip_map&#91;item].size)\n            astr = '\\n'.join('{0},{1},20201022'.format(str(e), item) for e in company_ip_map&#91;item])\n            fw.write( astr )\n            fw.write('\\n')\n    #\n#\n\n\nif __name__ == '__main__':\n    time_start = time.time()\n    try:\n        main()\n    except KeyboardInterrupt:\n        print('Killed by user')\n        sys.exit(0)\n    print(\"\\nSpend {0} seconds.\\n\".format(time.time() - time_start))\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>Python\u7684netaddr\u6a21\u5757\u4f7f\u7528\u8bb0\u5f55<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/2707.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/ixyzero.com\/blog\/archives\/2707.html<\/a><\/p>\n\n\n\n<p>\u963f\u91cc\u4e91IP\u6bb5\u7684\u6574\u7406<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/4961.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/ixyzero.com\/blog\/archives\/4961.html<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6700\u8fd1\u7684\u5de5\u4f5c\u4e2d\u6d89\u53ca\u5230\u4e00\u4e9bIP\u5c5e\u6027\u5206\u6790\u7684\u5185\u5bb9\uff0c\u5728\u6700\u540e\u5206\u6790\u6574\u7406\u7684\u5dee\u4e0d\u591a\u4e86\u4e4b\u540e\uff0c\u9700\u8981\u5c06\u5bf9\u5e94\u7684I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[1643,614,8,19,71],"class_list":["post-5019","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-ip","tag-netaddr","tag-python","tag-tips","tag-tools"],"views":3250,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5019"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5019\/revisions"}],"predecessor-version":[{"id":5020,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5019\/revisions\/5020"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}