Created
February 7, 2022 15:32
-
-
Save ezekg/c38836b8a461f16489417aadae4fc188 to your computer and use it in GitHub Desktop.
An example of validing a license key using Python 3's standard library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.request import Request, urlopen | |
import json | |
import sys | |
req = Request( | |
'https://api.keygen.sh/v1/accounts/demo/licenses/actions/validate-key', | |
method='POST', | |
data=json.dumps({ | |
'meta': { | |
'key': 'C1B6DE-39A6E3-DE1529-8559A0-4AF593-V3', | |
} | |
}).encode('ascii'), | |
headers={ | |
'Content-type': 'application/json', | |
'Accept': 'application/json', | |
}, | |
) | |
with urlopen(req) as response: | |
validation = json.load(response) | |
if validation['meta']['valid']: | |
print('License is valid!') | |
sys.exit(0) | |
else: | |
print('License is invalid: {constant}'.format(**validation['meta'])) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment