Set Expiration Date in python for specific keys

23 Views Asked by At

***The Problem is that the Key "4255" always refreshes when i type it in the console and it never expires, and how can i set the expiration date for many keys and not only one.

I want the Key to expire in the 1 minute and store that information somewhere so it doesnt refresh everytime i reuse the key.***

import random
from datetime import datetime, timedelta

def generate_key():
    key = str("4255")
    expiration_time = datetime.now() + timedelta(days=0, minutes=1) #Key expires in 1 day and 30 minutes
    return key, expiration_time

def is_expired(expiration_time):
    return datetime.now() > expiration_time

correct_key, expiration_time = generate_key()

print("# Generated key", correct_key)

user_input = input("Enter your license Key:")

if expiration_time == timedelta:
    print("your425 Key expired", (expiration_time))

if user_input == correct_key:
    print("Successfully Loaded")

if user_input == correct_key:
    print("Expiration:",(expiration_time))

else:
    print("Key Expired")

input("Press any Key to close")

The Key Should start the expiration date at the first use and somewhere store the information.

1

There are 1 best solutions below

0
Muhammed Samed Özmen On

Use dic to store key and expired time. This should work your code.

from datetime import datetime, timedelta
key_expiration_map = {}


def generate_key():
    key = str("4255")
    expiration_time = datetime.now() + timedelta(days=0, minutes=1) #Key expires in 1 day and 30 minutes
    return key, expiration_time

def is_expired(expiration_time):
    return datetime.now() > expiration_time


def validate_key(user_input):
    if user_input in key_expiration_map:
        expiration_time = key_expiration_map[user_input]
        if is_expired(expiration_time):
            print("Key Expired")
            return False
        else:
            print("Successfully Loaded")
            print("Expiration:", expiration_time)
            return True
    else:
        print("Invalid Key")
        return False

correct_key, expiration_time = generate_key()
key_expiration_map[correct_key] = expiration_time

print("# Generated key:", correct_key)

while True:
    user_input = input("Enter your license Key:")
    if validate_key(user_input):
        break

input("Press any Key to close")