{"id":567,"date":"2024-10-01T16:57:19","date_gmt":"2024-10-01T08:57:19","guid":{"rendered":"https:\/\/www.wzh994.cn\/?p=567"},"modified":"2025-09-10T09:44:20","modified_gmt":"2025-09-10T01:44:20","slug":"%e7%ba%af%e7%9c%9fip%e5%ba%93ip2region%ef%bc%8c%e6%8a%8aip%e5%9c%b0%e5%9d%80%e8%bd%ac%e6%8d%a2%e6%88%90%e7%9c%81%e5%b8%82%e5%8e%bf%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.wzh994.cn\/?p=567","title":{"rendered":"\u7eaf\u771fIP\u5e93+ip2region\uff0c\u628aIP\u5730\u5740\u8f6c\u6362\u6210\u7701\u5e02\u53bf\u7f16\u7801"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u53d1\u73b0\u9879\u76ee\u4f7f\u7528\u7684IP\u5730\u5740\u89e3\u6790\u5de5\u5177\uff0c\u51c6\u786e\u7387\u5f88\u4f4e\uff0c\u7136\u540e\u89e3\u6790\u51fa\u7684\u5730\u5740\u4fe1\u606f\u662f\u6587\u672c\uff0c\u5339\u914d\u7701\u5e02\u53bf\u4fe1\u606f\u8fd8\u9700\u8981\u590d\u6742\u7684\u89e3\u6790\uff0c\u6548\u7387\u6548\u679c\u90fd\u5f88\u5dee\uff0c\u4e8e\u662f\u51b3\u5b9a\u91cd\u6784\u8fd9\u4e00\u6a21\u5757\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u9605\u4e86\u5927\u91cf\u7f51\u4e0a\u7684\u65b9\u6848\uff0c\u5546\u4e1aAPI\u7684\u6548\u679c\u5f88\u597d\uff0c\u4f46\u6211\u4e2a\u4eba\u9879\u76ee\u4f7f\u7528\u6ca1\u6709\u5fc5\u8981\uff0c\u5e76\u4e14\u8981\u6c42\u4e5f\u6ca1\u6709\u90a3\u4e48\u9ad8\uff0c\u8fd8\u662f\u7528\u5f00\u6e90\u7684\u5427\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u6211\u627e\u5230\u4e86<a href=\"https:\/\/gitee.com\/lionsoul\/ip2region\">ip2region<\/a>\u5de5\u5177\uff0c\u9009\u62e9\u4e86\u4ed6\u7684java\u5b9e\u73b0\uff0c\u5f15\u5165\u4f9d\u8d56\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n    &lt;groupId&gt;org.lionsoul&lt;\/groupId&gt;\n    &lt;artifactId&gt;ip2region&lt;\/artifactId&gt;\n    &lt;version&gt;2.7.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u63a5\u4f7f\u7528\u4ed6\u4eec\u7684ip\u5730\u5740\u5e93\uff0c\u6413\u4e00\u4e2ademo\u8bd5\u4e00\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) throws Exception {\n    byte&#91;] buff = ResourceUtil.readBytes(\"ip2region.xdb\");\n    Searcher searcher = Searcher.newWithBuffer(buff);\n    String region = searcher.search(\"1.0.32.1\");\n    System.out.println(region);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u540e\u5f97\u5230\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4e2d\u56fd|0|\u5e7f\u4e1c\u7701|\u5e7f\u5dde\u5e02|\u7535\u4fe1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bd5\u4e86\u51e0\u4e2a\u5e38\u7528IP\u540e\u53d1\u73b0\u6570\u636e\u4e0d\u662f\u5f88\u51c6\u786e\uff0c\u5de5\u5177\u6587\u6863\u4e0a\u5df2\u7ecf\u6709\u8bf4\u660e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"681\" height=\"191\" src=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image.png\" alt=\"\" class=\"wp-image-575\" srcset=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image.png 681w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-300x84.png 300w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e76\u4e14<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-0-color\">ip2region.xdb<\/mark>\u8fd9\u4e2a\u6587\u4ef6\u6709\u70b9\u5927\u3002\u7b80\u5355\u5206\u6790\u4e86\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u6211\u4e0d\u9700\u8981\u56fd\u5916\u7684IP\u4fe1\u606f\u3002<br>2\u3001\u6211\u4e0d\u9700\u8981\u8fd0\u8425\u5546\u4fe1\u606f\u3002<br>3\u3001\u7701\u5e02\u53bf\u4fe1\u606f\u4f9d\u7136\u662f\u6587\u5b57\uff0c\u6ca1\u6709\u5feb\u901f\u83b7\u53d6\u5230\u7701\u5e02\u53bf\u7f16\u7801<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\u51b3\u5b9a\u81ea\u5df1\u6574\u7406\u751f\u6210\u4e00\u4e2axdb\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u59cb\u6570\u636e\u9009\u62e9\u4e86\u7eaf\u771f\u793e\u533a\u7248IP\u5e93(<a href=\"https:\/\/www.cz88.net\/\">https:\/\/www.cz88.net\/<\/a>)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"581\" src=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-1-1024x581.png\" alt=\"\" class=\"wp-image-580\" srcset=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-1-1024x581.png 1024w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-1-300x170.png 300w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-1-768x435.png 768w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-1.png 1365w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u83b7\u5f97\u6388\u6743\u540e\uff0c\u5f97\u5230\u4e86dat\u6570\u636e\u3002\u4e3a\u4e86\u67e5\u770b\u8be6\u7ec6\u4fe1\u606f\uff0c\u6211\u51b3\u5b9a\u5148\u628a\u8fd9\u4e2a\u6587\u4ef6\u89e3\u6790\u51fa\u6765\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u8003\u6587\u7ae0(<a href=\"https:\/\/www.iteye.com\/blog\/hzy3774-1851364\">https:\/\/www.iteye.com\/blog\/hzy3774-1851364<\/a>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;  \n  \n#define DAT_FILE \".\/qqwry20240731.dat\"  \n#define OUT_FILE \".\/out.txt\"  \n  \nFILE *out = NULL;  \n  \nvoid showIp(unsigned int ip)  \n{  \n    char buffer&#91;20] = {0};  \n    sprintf(buffer, \"%u.%u.%u.%u  \", (ip &gt;&gt; 24) &amp; 0xff, (ip &gt;&gt; 16) &amp; 0xff, (ip &gt;&gt; 8) &amp; 0xff, ip &amp; 0xff);  \n    fputs(buffer, out);  \n}  \n  \nvoid getString(FILE *fp)    \/\/\u8b80\u53d6\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u6587\u4ef6\uff0c\u4ee50\u4e3a\u7ed3\u5c3e\u6807\u5fd7  \n{  \n    char temp&#91;128] = {0};  \n    int index = 0;  \n    while ((temp&#91;index] = fgetc(fp)) != 0)  \n    {  \n        index++;  \n    }  \n    fputs(temp, out);  \n}  \n  \nvoid getInfo(FILE *fp, int flag)    \/\/\u83b7\u53d6\u6570\u636e\u533a\u5b57\u7b26\u4e32\u4fe1\u606f\uff0cflag\u4e3a1\u65f6\u8868\u793a\u5df2\u7ecf\u8bfb\u4e86\u4e00\u534a\u5b57\u7b26\u4e32  \n{  \n    unsigned char mod;  \n    long int offset = 0;  \n    long int preOffset = 0;  \n  \n    mod = fgetc(fp);            \/\/\u83b7\u53d6\u6a21\u5f0f  \n    if (mod &gt; 2)             \/\/\u5982\u679c\u65e0\u91cd\u5b9a\u5411  \n    {  \n        fseek(fp, -1L, SEEK_CUR);   \/\/\u8fd4\u56de  \n        getString(fp);          \/\/\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32  \n        if (flag == 0)          \/\/\u5982\u679c\u8c03\u7528\u51fd\u6570\u65f6\u8fd8\u672a\u8bfb  \n        {  \n            getInfo(fp, 1);     \/\/\u6807\u5fd7\u7f6e1\uff0c\u9012\u5f52\u8c03\u7528  \n        }  \n        return;  \n    }  \n    fread(&amp;offset, 3, 1, fp);       \/\/\u5982\u679c\u6709\u91cd\u5b9a\u5411\uff0c\u83b7\u53d6\u91cd\u5b9a\u5411\u504f\u79fb  \n    if (mod == 1)                   \/\/\u5982\u679c\u6a21\u5f0f\u4e00  \n    {  \n        fseek(fp, offset, SEEK_SET);    \/\/seek\u5230\u76ee\u6807\u5730\u5740  \n        getInfo(fp, flag);              \/\/\u9012\u5f52\u8bfb  \n        return;  \n    }  \n    preOffset = ftell(fp);          \/\/\u5982\u679c\u6a21\u5f0f\u4e8c\uff0c\u8bb0\u4f4f\u5f53\u524d\u504f\u79fb  \n    fseek(fp, offset, SEEK_SET);    \/\/\u5bfb\u76ee\u6807\u5730\u5740  \n    getInfo(fp, 1);                 \/\/\u8bfb\u4e00\u4e2a\u5b57\u7b26\u4e32  \n    if (flag == 0)                  \/\/\u5982\u679c\u8c03\u7528\u51fd\u6570\u65f6\u8fd8\u672a\u8bfb  \n    {  \n        fseek(fp, preOffset, SEEK_SET);     \/\/\u8fd8\u8981\u56de\u5230\u539f\u6765\u4f4d\u7f6e  \n        getInfo(fp, 1);                     \/\/\u518d\u8bfb\u4e00\u6b21  \n    }  \n}  \n  \nvoid parseRecord(FILE *fp, long int recordOffset)  \n{  \n    unsigned int endIp = 0;  \n  \n    fseek(fp, recordOffset, SEEK_SET);  \/\/seek\u5230\u6570\u636e\u533a  \n    fread(&amp;endIp, 4, 1, fp);        \/\/\u8bfb\u53d6\u7ed3\u675fIP  \n    showIp(endIp);                  \/\/\u663e\u793a\u7ed3\u675fIP  \n    getInfo(fp, 0);                 \/\/\u83b7\u53d6\u4fe1\u606f  \n    fputs(\"\\n\", out);  \n}  \n  \nvoid parseDat(FILE *fp)  \n{  \n    long int startIndexOffset = 0;  \n    long int endIndexOffset = 0;  \n    long int indexOffset = 0;  \n    long int recordOffset = 0;  \n    unsigned int startIp = 0;  \n  \n    fseek(fp, 0L, SEEK_SET);            \/\/seek\u6587\u4ef6\u5934  \n    fread(&amp;startIndexOffset, 4, 1, fp); \/\/\u8bfb\u7d22\u5f15\u5f00\u59cb\u504f\u79fb\u91cf  \n    fread(&amp;endIndexOffset, 4, 1, fp);   \/\/\u8bfb\u7d22\u5f15\u7ed3\u675f\u504f\u79fb\u91cf  \n    indexOffset = startIndexOffset;  \n    while (indexOffset &lt;= endIndexOffset)    \/\/\u5faa\u73af\u4ece\u6839\u636e\u7d22\u5f15\u8bfb\u53d6\u6570\u636e  \n    {  \n        fseek(fp, indexOffset, SEEK_SET);   \/\/seek\u5230\u7d22\u5f15\u4f4d\u7f6e  \n        fread(&amp;startIp, 4, 1, fp);          \/\/\u8bfb\u53d6\u7d22\u5f15\u4e2d\u7684\u5f00\u59cbIP  \n        fread(&amp;recordOffset, 3, 1, fp);     \/\/\u8bfb\u53d6\u5bf9\u5e94\u6570\u636e\u533a\u7684\u504f\u79fb\u91cf  \n        indexOffset = ftell(fp);            \/\/\u8bb0\u4f4f\u7d22\u5f15\u7684\u504f\u79fb\u91cf  \n        showIp(startIp);                    \/\/\u663e\u793a\u5f00\u59cbIP  \n        parseRecord(fp, recordOffset);      \/\/\u8bfb\u53d6\u6570\u636e\u533a  \n    }  \n}  \n  \nint main(void)  \n{  \n    FILE *fp = NULL;  \n  \n    out = fopen(OUT_FILE, \"wb\");  \n    fp = fopen(DAT_FILE, \"rb\");  \n    if (fp == NULL )  \n    {  \n        printf(\"open file error!\\n\");  \n        return 0;  \n    }  \n    parseDat(fp);  \n    fclose(fp);  \n    return 0;  \n}  <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\u5f97\u5230\u4e86\u4e00\u4e2a80+MB\u7684\u8d85\u5927\u6587\u4ef6<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"578\" src=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-4.png\" alt=\"\" class=\"wp-image-586\" srcset=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-4.png 859w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-4-300x202.png 300w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-4-768x517.png 768w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5206\u6790\u4e86\u4e00\u4e0b\u7ed3\u6784\uff0c\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u820d\u5f03\u6389\u4e86\u56fd\u5916\u7684\u6570\u636e\uff0c\u7136\u540e\u79fb\u9664\/\u540e\u9762\u7684\u7cbe\u786e\u5730\u5740\uff0c\u6700\u7ec8\u4fdd\u7559\u7701\u5e02\u53bf\u540d\u79f0\u548c2\u4f4d\u884c\u653f\u7f16\u7801\u3002\u5982\u6e56\u5357\u7701430000\uff0c\u53ea\u5b58\u50a843<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json,re\n# \u52a0\u8f7d\u7701\u5e02\u53bf\u6570\u636e\nwith open(\"region.json\", 'r', encoding='utf-8') as file:\n    data = json.load(file)\nprint(\"---\u7701\u5e02\u53bf\u52a0\u8f7d\u5b8c\u6210---\")\n\nwith open('ip2region.txt', 'w', encoding='utf-8') as outfile:\n    with open('out.txt', 'r') as file:\n        line = file.readline()\n        while line:\n            # \u53bb\u6389\u56fd\u5916\u7684\n            if \"\u4e2d\u56fd\" not in line:\n                line = file.readline()\n                continue\n            line = line.replace(\"\u4e2d\u56fd\u2013\",\"\")\n            line = line.split(\"  \")\n            # \u89e3\u6790\u7701\u5e02\u53bf \u79fb\u9664\u4e0d\u5fc5\u8981\u7684\u6570\u636e\n            # \/\u540e\u9762\u7684\u662f\u7cbe\u786e\u5730\u5740\uff0c\u820d\u5f03\n            area = line&#91;2].split(\"\u2013\")\n            containProvince = None\n            containCity = None\n            containCounty = None\n            # \u5339\u914d\u7701\n            for province in data:\n                if re.search(province&#91;\"short_name\"], area&#91;0]) is not None:\n                    # \u627e\u5230\u4e86\u7701\n                    containProvince = province\n                    # \u6709\u7b2c\u4e8c\u7ea7\u5730\u533a\n                    if(len(area)&gt;2):\n                        for city in province&#91;\"children\"]:\n                            if re.search(city&#91;\"short_name\"], area&#91;1]) is not None:\n                                # \u627e\u5230\u4e86\u7701\n                                containCity = city\n                                if(len(area)&gt;3):\n                                    for county in city&#91;\"children\"]:\n                                        if re.search(county&#91;\"short_name\"], area&#91;2]) is not None:\n                                            # \u627e\u5230\u4e86\u7701\n                                            containCounty = county\n\n            if containProvince is None:\n                #\u6ca1\u6709\u7701\u7684\u6570\u636e \u8df3\u8fc7\n                continue\n            # \u53ea\u4fdd\u7559\u7701\u5e02\u53bf\uff0c\u5176\u4ed6\u4fe1\u606f\u4e22\u5f03\n            result=line&#91;0]+\"|\"+line&#91;1]+\"|\"+containProvince&#91;\"region_name\"]+\"|\"+str(containProvince&#91;\"id\"])&#91;:2]\n            if containCity is not None:\n                result = result +\"|\"+containCity&#91;\"region_name\"]+\"|\"+str(containCity&#91;\"id\"])&#91;3:4]\n            if containCounty is not None:\n                result = result +\"|\"+containCounty&#91;\"region_name\"]+\"|\"+str(containCounty&#91;\"id\"])&#91;5:6]\n            result=result+\"\\n\"\n            outfile.write(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5c31\u5f97\u5230\u4e86\u8fd9\u6837\u7684\u6570\u636e\uff0c\u670915W\u591a\u6761\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"416\" height=\"332\" src=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-5.png\" alt=\"\" class=\"wp-image-587\" srcset=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-5.png 416w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-5-300x239.png 300w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5229\u7528ip2region\u63d0\u4f9b\u7684\u6570\u636e\u751f\u6210python\u5b9e\u73b0(<a href=\"https:\/\/gitee.com\/lionsoul\/ip2region\/tree\/master\/maker\/python\">https:\/\/gitee.com\/lionsoul\/ip2region\/tree\/master\/maker\/python<\/a>)\uff0c\u8f6c\u6210XDB\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python genxdb.py gen --src=.\/ip2region.txt --dst=.\/ip2region.xdb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u671f\u95f4\u62a5\u4e86\u4e00\u4e2a\u9519\u8bef\uff0cmake.py155-161\u884c\uff0c\u4e0a\u4e00\u884c\u7684\u7ed3\u675fIP\u548c\u4e0b\u4e00\u884c\u7684\u8d77\u59cbIP\u4e0d\u8fde\u7eed\uff0c\u76f4\u63a5\u6ce8\u91ca\u6389\u8fd9\u6bb5\u4ee3\u7801<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"194\" src=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-6-1024x194.png\" alt=\"\" class=\"wp-image-588\" srcset=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-6-1024x194.png 1024w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-6-300x57.png 300w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-6-768x146.png 768w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-6.png 1224w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5c31\u5f97\u5230\u4e86\u65b0\u7684xdb\u6587\u4ef6\uff0c\u53ea\u67092.6MB\uff0c\u5c0f\u4e86\u5f88\u591a\uff0c\u68c0\u67e5\u53d1\u73b0\uff0c\u8fd8\u53ef\u4ee5\u66f4\u52a0\u7cbe\u7b80\uff0c\u7531\u4e8e\u53bb\u6389\u4e86\u8be6\u7ec6\u5730\u5740\u548c\u8fd0\u8425\u5546\u7b49\u4fe1\u606f\uff0c\u5f88\u591a\u5730\u5740\u4fe1\u606f\u662f\u76f8\u540c\u4e14\u8fde\u7eed\u7684\uff0c\u53ef\u4ee5\u5408\u5e76\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"566\" height=\"479\" src=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-8.png\" alt=\"\" class=\"wp-image-590\" srcset=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-8.png 566w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-8-300x254.png 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\u6211\u4fee\u6539\u4e86maker.py\u4e2d\u7684load_segments\u51fd\u6570\uff0c\u589e\u52a0\u4e86\u5408\u5e76\u8fde\u7eedIP\u76f8\u540c\u5730\u5740\u7684\u903b\u8f91\uff0c\u5408\u5e76\u540e\uff0c\u6570\u636e\u51cf\u5c11\u52306W\u6761\uff0c\u751f\u6210\u7684XDB\u6587\u4ef6\u4ec51MB\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            if last is not None:\n                if last.end_ip + 1 == segment.start_ip:\n                    if last.region == segment.region:\n                        # \u79fb\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20,\u4fee\u6539\u5f53\u524d\u5143\u7d20\u7684\u8d77\u59cbIP\n                        self.segments.pop()\n                        segment.start_ip = last.start_ip<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\uff0c\u91cd\u65b0\u7f16\u5199\u4e86\u89e3\u6790\u5de5\u5177\u7c7b\uff0c\u5c31\u53ef\u4ee5\u81ea\u52a8\u89e3\u6790\u51fa\u884c\u653f\u7f16\u7801\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static IP2Region getIPAttribution(String ip) {\n    IP2Region ipAttribution = new IP2Region();\n    ipAttribution.setIp(ip);\n    try {\n\n        Searcher searcher = Searcher.newWithBuffer(ResourceUtil.readBytes(\"META-INF\/ip2region.xdb\"));\n\n        String region = searcher.search(ip);\n\n        searcher.close();\n\n        String&#91;] result = region.split(\"\\\\|\");\n        if (ObjectUtil.isNotNull(result&#91;0])) {\n            ipAttribution.setProvince(result&#91;0]);\n            ipAttribution.setProvinceId(StrUtil.padAfter(result&#91;1], 6, \"0\"));\n        }\n        if (ObjectUtil.isNotNull(result&#91;2])) {\n            ipAttribution.setCity(result&#91;2]);\n            ipAttribution.setCityId(StrUtil.padAfter(result&#91;1] + StrUtil.padPre(result&#91;3], 2, \"0\"), 6, \"0\"));\n        }\n        if (ObjectUtil.isNotNull(result&#91;4])) {\n            ipAttribution.setCounty(result&#91;4]);\n            ipAttribution.setCountyId(result&#91;1] + StrUtil.padPre(result&#91;3], 2, \"0\") + StrUtil.padPre(result&#91;5], 2, \"0\"));\n        }\n    } catch (Exception ignore) {\n    }\n    return ipAttribution;\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"233\" src=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-9.png\" alt=\"\" class=\"wp-image-591\" srcset=\"https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-9.png 350w, https:\/\/www.wzh994.cn\/wp-content\/uploads\/2024\/08\/image-9-300x200.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u53d1\u73b0\u9879\u76ee\u4f7f\u7528\u7684IP\u5730\u5740\u89e3\u6790\u5de5\u5177\uff0c\u51c6\u786e\u7387\u5f88\u4f4e\uff0c\u7136\u540e\u89e3\u6790\u51fa\u7684\u5730\u5740\u4fe1\u606f\u662f\u6587\u672c\uff0c\u5339\u914d\u7701\u5e02\u53bf\u4fe1\u606f\u8fd8\u9700\u8981\u590d\u6742\u7684\u89e3\u6790\uff0c\u6548 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[],"class_list":["post-567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-15"],"_links":{"self":[{"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=567"}],"version-history":[{"count":16,"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":601,"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=\/wp\/v2\/posts\/567\/revisions\/601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=\/wp\/v2\/media\/591"}],"wp:attachment":[{"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wzh994.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}