You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.8KB

  1. import time,re,hashlib
  2. from config import load_config,md5,conf
  3. import config
  4. def get_file_md5(file_name):
  5. """
  6. 计算文件的md5
  7. :param file_name:
  8. :return m.hexdigest():
  9. """
  10. m = hashlib.md5() #创建md5对象
  11. with open(file_name,'rb') as fobj:
  12. while True:
  13. data = fobj.read(1024)
  14. if not data:
  15. break
  16. m.update(data) #更新md5对象
  17. return m.hexdigest() #返回md5值
  18. def time_checker(f):
  19. # print(args[0]())
  20. def wrapTheFunction(self, *args, **kwargs):
  21. global md5 # 从config.py拿来一个全局变量md5 默认是False
  22. if md5 == None:
  23. _config = conf()
  24. elif md5 == get_file_md5("./config.json"):
  25. _config = conf()
  26. # chat_time_module = _config["chat_time_module"]
  27. # chat_start_time = _config["chat_start_time"]
  28. # chat_stopt_time = _config["chat_stop_time"]
  29. else:
  30. print("检测到配置文件变化")
  31. _config = load_config() # 启动时间支持热更改 修改config.json文件后即可生效
  32. md5 = get_file_md5("./config.json")
  33. # config.md5 = get_file_md5("./config.json")
  34. chat_time_module = _config["chat_time_module"]
  35. chat_start_time = _config["chat_start_time"]
  36. chat_stopt_time = _config["chat_stop_time"]
  37. # print(md5,chat_time_module,chat_start_time,chat_stopt_time)
  38. if chat_time_module:
  39. time_regex = re.compile(r'^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$') #时间匹配,包含24:00
  40. starttime_format_check = time_regex.match(chat_start_time) # 检查停止时间格式
  41. stoptime_format_check = time_regex.match(chat_stopt_time) # 检查停止时间格式
  42. chat_time_check = chat_start_time < chat_stopt_time # 确定启动时间<停止时间
  43. if starttime_format_check and stoptime_format_check and chat_time_check:
  44. # print('服务启动时间:{}'.format(CHAT_START_TIME))
  45. # print('服务结束时间:{}'.format(CHAT_STOP_TIME))
  46. if chat_start_time>"23:59":
  47. print('启动时间可能存在问题,请修改')
  48. else:
  49. print("时间格式不正确,请在config.json中修改您的CHAT_START_TIME/CHAT_STOP_TIME,否则可能会影响您正常使用,程序正在自动退出")
  50. now_time = time.strftime("%H:%M", time.localtime())
  51. if chat_start_time <= now_time <= chat_stopt_time:
  52. # print("在服务时间内")
  53. # 正常请求并返回
  54. f(self, *args, **kwargs)
  55. return None
  56. else:
  57. print('不在服务时间内,禁止访问')
  58. return None
  59. else:
  60. f(self, *args, **kwargs)
  61. return wrapTheFunction