# encoding=utf8
import requests
import hashlib,sys,os,shutil,ipdb
down_url = "https://user.ipip.net/download.php?a=custom&token=TOKEN&lang=CN&name=ipv4_cn.ipdb.zip" #新版本下载地址
response = requests.get(down_url) # 发起http请求
if response.status_code != 200:
print(response.content.decode('utf-8')) # 错误提示
sys.exit(0)
etag_value = response.headers.get("ETag") #获取ETag值
if not etag_value: # ETag不存在就退出
print("etag not exists")
sys.exit(0)
with open("c:/tmp/ipip_temp.ipdb", 'wb+') as fd: #写临时文件
for chunk in response.iter_content(4096):
fd.write(chunk)
with open("c:/tmp/ipip_temp.ipdb", 'rb') as fd: #读取临时文件
sha1 = hashlib.sha1()
while True:
content = fd.read(4096)
if not content:
break
sha1.update(content)
content_sha1_value = sha1.hexdigest() #计算临时文件sha1
etag_sha1_value = etag_value[5:]
if etag_sha1_value != content_sha1_value: # sha1 不一致退出
print("etag err")
sys.exit(0)
ipdb.City("c:/tmp/ipip_temp.ipdb") # ipdb自检,可能 Error,则不覆盖 重新下载。
shutil.copyfile("c:/tmp/ipip_temp.ipdb", "c:/tmp/ipip.ipdb") # 覆盖正式文件,目标目录必须有可写权限。
print("ok")
© 2013 - 2025 北京天特信科技有限公司 所有权利保留
本网站 SSL 证书由 TRUSTASIA 提供
本网站安全防护服务由 上海云盾 提供