def get_unique_code():
# get code from env
code_from_env = os.environ.get("STRACK_UNIQUE_CODE")
if code_from_env:
return code_from_env
# read code from cache
unique_code_cache_file = os.path.join(STRACK_USER_PATH, "unique")
if os.path.isfile(unique_code_cache_file):
with open(unique_code_cache_file) as f:
code_from_file = f.read()
os.environ.update({"STRACK_UNIQUE_CODE": code_from_file})
return code_from_file
# make dir
if not os.path.isdir(STRACK_USER_PATH):
os.makedirs(STRACK_USER_PATH)
# generate the code
computer_name = socket.getfqdn(socket.gethostname())
ip = socket.gethostbyname(computer_name)
unique_code = md5(ip).hexdigest()
# save cache
os.environ.update({"STRACK_UNIQUE_CODE": unique_code})
with open(unique_code_cache_file, "w") as f:
f.write(unique_code)
return unique_code
评论列表
文章目录