1234567891011121314151617181920212223242526272829303132 |
- # -*- coding: utf-8 -*-
- # @Author: wangkun
- # @Time: 2023/7/14
- import random
- import string
- # 函数 1 生成随机 token
- def generate_token():
- token = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
- return token
- # 函数 2 和函数 3 使用同一个 token,当 token 过期时重新获取
- def function_2():
- global token
- if not token:
- token = generate_token()
- print("函数 2 使用 token:", token)
- def function_3():
- global token
- if not token:
- token = generate_token()
- print("函数 3 使用 token:", token)
- # 测试
- token = generate_token()
- print("初始 token:", token)
- function_2()
- # 模拟 token 过期
- token = None
- function_2()
- function_3()
|