[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"article-3":3},{"id":4,"title":5,"title_en":6,"abstract":7,"abstract_en":8,"content":9,"content_en":10,"category":11,"banner_id":12,"banner_path":13,"tags":14,"is_recommend":16,"prev_article":17,"next_article":21,"created_at":25},3,"根据ip获取城市","Get the city according to IP","获取ip\n\n```Python\nx_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')  # 判断是否使用代理\nif x_forwarde","Get the IP\n\n```Python\nx_forwarded_for = request. META.get('HTTP_X_FORWARDED_FOR') # Determine whether to use a proxy\nif x_forwarde","获取ip\n\n```Python\nx_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')  # 判断是否使用代理\nif x_forwarded_for:\n    ip = x_forwarded_for.split(',')[0]  # 使用代理获取真实的ip\nelse:\n    ip = request.META.get('REMOTE_ADDR')  # 未使用代理获取IP\n\nprint(ip)\n```\n\n\n\n\n\n获取城市\n\n```Python\nimport requests\nimport re\n\nheaders = {\n    \"user-agent\": \"Mozilla\u002F5.0 (Windows NT 10.0; Win64; x64) \"\n                  \"AppleWebKit\u002F537.36 (KHTML, like Gecko) \"\n                  \"Chrome\u002F98.0.4758.102 Safari\u002F537.36\"\n}\n\n\ndef get_city(ip: str) -&gt; dict:\n    \"\"\"\n    合法 ip 返回国家，城市，和运营商\n    查不到运营商的就返回 国家，城市\n    内网 ip返回 内网\n    :param ip:\n    :return:\n    \"\"\"\n    # 构造返回的字典\n    response = {\n        \"country\": '',\n        \"city\": '',\n    }\n    # 判断是否是内网 ip\n    if ip.startswith('127') or ip.startswith('192.') or ip.startswith('10.'):\n        response['msg'] = '内网'\n        return response\n    url = f'https:\u002F\u002Fip.cn\u002Fip\u002F{ip}.html'\n    res = requests.get(url=url, headers=headers)\n    result: str = re.findall(r'\u003Cdiv id=\"tab0_address\">(.*?)\u003C\u002Fdiv>', res.text)[0].strip()\n\n    res_list = result.split('  ')\n    response['country'] = res_list[0]\n    city_or_operator = res_list[1]\n    city = city_or_operator.split(' ')\n    response['city'] = city[0] + '-' + city[1]\n    if len(city) == 3:\n        response['operator'] = city[2]\n    return response\n\n\nif __name__ == '__main__':\n    print(get_city('127.0.0.1'))\n    print(get_city('192.168.44.33'))\n    print(get_city('117.136.24.38'))\n    print(get_city('218.76.35.2'))\n    print(get_city('110.53.253.137'))\n    print(get_city('36.99.136.139'))\n    print(get_city('106.13.185.190'))\n    print(get_city('42.194.130.81'))\n\n```","Get the IP\n\nif x_forwarded_for:\n```Python\nx_forwarded_for = request. META.get('HTTP_X_FORWARDED_FOR') # Determine whether to use a proxy\nip = x_forwarded_for.split(',')[0] # Use a proxy to get the real IP\nelse:\nip = request. META.get('REMOTE_ADDR') # No proxy is used to get the IP\n\nprint(ip)\n```\n\n\n\n\n\n获取城市\n\n```Python\nimport requests\nimport re\n\nheaders = {\n    \"user-agent\": \"Mozilla\u002F5.0 (Windows NT 10.0; Win64; x64) \"\n                  \"AppleWebKit\u002F537.36 (KHTML, like Gecko) \"\n                  \"Chrome\u002F98.0.4758.102 Safari\u002F537.36\"\n}\n\n\ndef get_city(ip: str) -&gt; dict:\n    \"\"\"\n    合法 ip 返回国家，城市，和运营商\n    查不到运营商的就返回 国家，城市\n    内网 ip返回 内网\n    :param ip:\n    :return:\n    \"\"\"\n    # 构造返回的字典\n    response = {\n        \"country\": '',\n        \"city\": '',\n    }\n    # 判断是否是内网 ip\n    if ip.startswith('127') or ip.startswith('192.') or ip.startswith('10.'):\n        response['msg'] = '内网'\n        return response\n    url = f'https:\u002F\u002Fip.cn\u002Fip\u002F{ip}.html'\n    res = requests.get(url=url, headers=headers)\n    result: str = re.findall(r'\u003Cdiv id=\"tab0_address\">(.*?)\u003C\u002Fdiv>', res.text)[0].strip()\n\n    res_list = result.split('  ')\n    response['country'] = res_list[0]\n    city_or_operator = res_list[1]\n    city = city_or_operator.split(' ')\n    response['city'] = city[0] + '-' + city[1]\n    if len(city) == 3:\n        response['operator'] = city[2]\n    return response\n\n\nif __name__ == '__main__':\n    print(get_city('127.0.0.1'))\n    print(get_city('192.168.44.33'))\n    print(get_city('117.136.24.38'))\n    print(get_city('218.76.35.2'))\n    print(get_city('110.53.253.137'))\n    print(get_city('36.99.136.139'))\n    print(get_city('106.13.185.190'))\n    print(get_city('42.194.130.81'))\n\n```","python",0,"https:\u002F\u002Fblog4-1316398321.cos.ap-nanjing.myqcloud.com\u002Fblog5\u002F20250715062906__【哲风壁纸】动漫角色-未来世界.png",[15],"Python",false,{"id":18,"title":19,"title_en":20},2,"Django指定IP访问（限制IP访问）","Django specifies IP access (restricts IP access)",{"id":22,"title":23,"title_en":24},4,"Python条件语句","Python conditional statements","2025-07-21T05:01:39+08:00"]