Skip to content

Instantly share code, notes, and snippets.

@ezekg
Created February 7, 2022 15:32
Show Gist options
  • Save ezekg/c38836b8a461f16489417aadae4fc188 to your computer and use it in GitHub Desktop.
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
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