Skip to content

Instantly share code, notes, and snippets.

@afternoon
Last active April 29, 2017 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save afternoon/5310922 to your computer and use it in GitHub Desktop.
Save afternoon/5310922 to your computer and use it in GitHub Desktop.
Generate URL-safe UUID/GUIDs in Python.
#!/usr/bin/python
# Generate URL-safe UUID/GUIDs in Python, e.g.
#
# ob9G9Ju_Re6SRgxacdUzhw
# k0CWKgThQq-9b2ZcmpVXXA
#
# base64 has an urlsafe encoding
from base64 import urlsafe_b64encode
# any of uuid1, uuid3, uuid4 or uuid5 will do
# see http://docs.python.org/2/library/uuid.html
from uuid import uuid4 as uuid
# generate a uuid and base64 encode it
# drop trailing "==", add it back if decoding
print urlsafe_b64encode(uuid().bytes)[0:22]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment