Token, certificate. For HTTP connections, the main function is to distinguish the (when) The request logs on. Therefore, it must meet the following requirements:
- Uniqueness. Different users have a unique token for each login.
- Valid time. The token expires.
- Start time. No, this cannot be used to check whether it is invalid.
Based on the above three points, a simple Token Generation and verification is compiled:
1 import time 2 Import base64 3 4 class token: 5 # playerid can be replaced with your own string to ensure that each user has a different 6 def gettoken (self, playid, delaytime ): 7 curtime = int (time. time () 8 # You can modify 9 mystr = STR (playid) + '$' + STR (curtime) + '$' + STR (delaytime) 10 token = base64.encodestring (mystr ). strip () 11 return token12 13 # Check whether the time is valid. 14 def istokenvalible (self, token): 15 base64text = base64.decodestring (token) 16 strlist = base64text. split ('$') 17 curtime = int (time. time () 18 starttime = int (strlist [1]) 19 if curtime-starttime> int (strlist [2]): 20 return false21 else: 22 return true
Cocos2dx Network (2) Token Generation