dev.py 716 B

1234567891011121314151617181920212223242526272829303132
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/7/14
  4. import random
  5. import string
  6. # 函数 1 生成随机 token
  7. def generate_token():
  8. token = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
  9. return token
  10. # 函数 2 和函数 3 使用同一个 token,当 token 过期时重新获取
  11. def function_2():
  12. global token
  13. if not token:
  14. token = generate_token()
  15. print("函数 2 使用 token:", token)
  16. def function_3():
  17. global token
  18. if not token:
  19. token = generate_token()
  20. print("函数 3 使用 token:", token)
  21. # 测试
  22. token = generate_token()
  23. print("初始 token:", token)
  24. function_2()
  25. # 模拟 token 过期
  26. token = None
  27. function_2()
  28. function_3()