Python key gen that can be used 1 time and check its valid


Hey im learning python and i would like to make a script that requires a licence key, but i want to make it require a personal key, for example they would have to purchase the programme/key to then proceed to the next stage. but i want it so that the key is generated and can only be used once.

activeKey = "Test"

enterActiveKey = input("Please enter the activation key down below:\n")
if activeKey == enterActiveKey:
print("Activation key is successful.")
while activeKey != enterActiveKey:
print("Activation key is incorrect. Please re-run the program")

I made this script that i can specifically chose a activation key but i want it so that the key is generated and only possible to be used 1 time so i dont need to specify the key.

Asked By: Biggles Devs



This cannot be done,because unique id generation is not possible, even UUID have collision risk,
So the best way is maintaining a Database,
As you say user have to purchase the key, this means you have a website so you can try
mongodb atlas(free for some extend ) even sqlite,
any kind of database is ok,
tutorial for database connectivity

Here is the very basic flow:


  1. You generate any uuid key then store that in database
  2. When user purchase a key, update the database that this key is sold
  3. Now when user enter that key in your program check Database that if
    this key is present and sold

you can now add your logic to make it better.

Answered By: Somen Das
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.