Skip to content

Commit 4886f6e

Browse files
author
Franz Ludwig Kostelezky
committed
Update licensing functionality and server configuration
1 parent fb77205 commit 4886f6e

5 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
server/key.pem
22
server/cert.pem
3+
.license_key
4+
server/__pycache__/
5+
python_licensing/__pycache__/

‎example.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from python_licensing.python_licensing import licensed
22

3-
@licensed('https://localhost:5000')
3+
@licensed('https://127.0.0.1:5000')
44
def main():
5-
print("This is a licensed function.")
5+
print("The body of the licensed function has been executed.")
66
input("Press any key to exit.")
77

88
if __name__ == '__main__':
9-
main()
9+
main()

‎python_licensing/python_licensing.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ def get_activation_key():
1818
Get the activation key. If the key is not stored locally, ask the user to enter it.
1919
Store the key locally for future use.
2020
"""
21-
key_file = os.path.join(os.path.expanduser('~'), '.license_key')
21+
key_file = os.path.join(os.path.expanduser('.'), '.license_key')
2222
if os.path.exists(key_file):
23-
with open(key_file, 'r') as f:
23+
with open(key_file, 'r', encoding='utf-8') as f:
2424
return f.read().strip()
2525
else:
2626
key = input("Enter your activation key: ")
27-
with open(key_file, 'w') as f:
27+
with open(key_file, 'w', encoding='utf-8') as f:
2828
f.write(key)
2929
return key
3030

@@ -38,13 +38,13 @@ def wrapper(*args, **kwargs):
3838
activation_key = get_activation_key()
3939
try:
4040
response = requests.get(
41-
server_url,
41+
server_url + '/check_license',
4242
params={
4343
'hash': unique_hash,
4444
'key': activation_key
4545
},
46-
verify=False,
47-
timeout=20)
46+
verify=False,
47+
timeout=20)
4848
if response.status_code == 200 and response.json()['valid']:
4949
return func(*args, **kwargs)
5050
except requests.exceptions.Timeout:

‎server/server.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ def check_license(license_hash, key):
4141
conn_ = connect_to_db()
4242

4343
cur = conn_.cursor()
44-
cur.execute("SELECT * FROM licenses WHERE hash = %s AND activation_key = %s",
45-
(license_hash, key))
44+
cur.execute("SELECT * FROM licenses WHERE activation_key = %s",
45+
(key,))
4646
result = cur.fetchone()
47+
print(key, result)
4748
if result is None:
4849
return False
4950
return result[0]
@@ -62,7 +63,7 @@ def handle_check_license():
6263
and 'message' (a string with a message about the license status).
6364
"""
6465
license_hash = request.args.get('hash')
65-
key = request.args.get('activation_key')
66+
key = request.args.get('key')
6667
valid = check_license(license_hash, key)
6768
return {'valid': valid}
6869

‎server/server_tools.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def initialize_db(conn):
5353
cur.execute("""
5454
CREATE TABLE IF NOT EXISTS licenses (
5555
id SERIAL PRIMARY KEY,
56-
hash TEXT NOT NULL,
56+
hash TEXT NOT NULL UNIQUE,
5757
expires_after TIMESTAMP NOT NULL,
58-
activation_key TEXT NOT NULL,
58+
activation_key TEXT NOT NULL UNIQUE,
5959
activated_on TIMESTAMP
6060
)
6161
""")
@@ -73,7 +73,7 @@ def add_entry(conn, entry):
7373
""",
7474
(entry['id'],
7575
entry['hash'],
76-
datetime.now() + timedelta(seconds=int(entry['expires_after'])),
76+
entry['expires_after'],
7777
entry['activation_key'],
7878
entry['activated_on'])
7979
)
@@ -115,7 +115,7 @@ def main():
115115
parser.add_argument('--add',
116116
metavar='ENTRY',
117117
help='''Add an entry to the database.
118-
Example: --add "id=1 hash=hash expires_after=1234
118+
Example: --add "id=1 hash=hash expires_after=2024-02-01
119119
activation_key=activation-key activated_on=2024-01-01"''')
120120
parser.add_argument('--remove',
121121
metavar='ID',

0 commit comments

Comments
 (0)