$ curl cheat.sh/
#  You can use like as mentioned the builtin `uuid` module. The new
#  `secrets` module released in 3.6 is also capable of creating unique
#  tokens also.

 from uuid import uuid4

 rand_token = uuid4()

#  The function below creates a unique token every time it's called. The
#  `os.urandom` method returns 20 random bytes as a string and the
#  `binascii.hexlify` method converts each of those 20 bytes into 2-digit
#  hex representation of that byte. This is why the return value is twice
#  as long.
#  
#  If you want to use this approach and need tokens to be specific
#  length, use **half** of the length you need as an argument to the
#  `os.urandom` method.

 def generate_key(self):
     return binascii.hexlify(os.urandom(20)).decode()

#  [Sean Parsons] [so/q/41354205] [cc by-sa 3.0]

$
Follow @igor_chubin cheat.sh