Skip to content

Instantly share code, notes, and snippets.

@transmissions11
Last active October 27, 2025 02:50
Show Gist options
  • Select an option

  • Save transmissions11/a58605041b7032430058b9fe4ae63827 to your computer and use it in GitHub Desktop.

Select an option

Save transmissions11/a58605041b7032430058b9fe4ae63827 to your computer and use it in GitHub Desktop.
scripts.mit.edu webscript for putting basic authorization in front of a JSON file
#!/usr/bin/python
# Simple scripts.mit.edu webscript for putting
# basic authorization in front of a JSON file.
# Put this in a public/ subdir of web_scripts
# and your private data in a private/ subdir.
import os, io, json, sys
EXPECTED_FILE_KEY = "<password>"
PRIVATE_JSON = "<filename>.json"
if __name__ == "__main__":
key = os.environ.get("HTTP_X_FILE_KEY", "")
if key == EXPECTED_FILE_KEY:
p = os.path.join(os.path.dirname(__file__), '..', 'private', PRIVATE_JSON)
print "Content-Type: application/json"
print "Cache-Control: no-store\n"
s = io.open(p, 'r', encoding='utf-8').read()
sys.stdout.write(json.dumps(json.loads(s)))
else:
print "Status: 401 Unauthorized"
print "Content-Type: text/plain; charset=utf-8"
print "Cache-Control: no-store\n"
print "nope: %s key" % ("bad" if key else "missing")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment