Cocos2dx Network (2) Token Generation

Source: Internet
Author: User

Token, certificate. For HTTP connections, the main function is to distinguish the (when) The request logs on. Therefore, it must meet the following requirements:

  1. Uniqueness. Different users have a unique token for each login.
  2. Valid time. The token expires.
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.