Skip to content

Instantly share code, notes, and snippets.

@duhaime
Last active June 18, 2020 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duhaime/50ec24847c1b619d52c927904d92e50c to your computer and use it in GitHub Desktop.
Save duhaime/50ec24847c1b619d52c927904d92e50c to your computer and use it in GitHub Desktop.
Simple Flask Server
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>title</title>
<style>
* {
margin: 0;
padding: 0;
}
body,
html {
background: #0d1750;
color: #fff;
padding: 40px;
}
input {
padding: 10px;
font-size: 17px;
width: 200px;
}
button {
padding: 12px;
font-size: 16px;
border: none;
}
</style>
</head>
<body>
<!-- for more on inputs check out: https://www.w3schools.com/html/html_forms.asp -->
<input type='text' placeholder='type your email address'></input>
<button>SUBMIT</button>
<script>
function get(url, handleSuccess, handleErr, handleProgress) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status === 200) {
if (handleSuccess) handleSuccess(xmlhttp.responseText)
} else {
if (handleErr) handleErr(xmlhttp)
}
};
};
xmlhttp.open('GET', url, true);
xmlhttp.send();
};
// bind event listener
var button = document.querySelector('button');
button.addEventListener('click', function() {
// read the value of the input
var inputValue = document.querySelector('input').value;
// make a request to /new_key and send the user's email
get('http://localhost:5050/new_key?email=' + inputValue, function(data) {
data = JSON.parse(data);
alert('your key is ' + data.key)
})
})
</script>
</body>
</html>
import json, random, os
from flask import Flask, jsonify, request, send_from_directory
app = Flask(__name__, static_url_path='')
if not os.path.exists('keys.json'):
with open('keys.json', 'w') as out:
json.dump({}, out)
def load_keys():
'''
Return a list of the keys in keys.json
'''
with open('keys.json') as f:
return json.load(f)
@app.route('/new_key')
def new_key():
'''
Read the users email from query params and make a new key
'''
email = request.args.get('email', '')
# make a new key that's a string of an integer between 0 and 2^32
keys = load_keys()
new_key = str(random.randint(0, 2**32))
keys[email] = new_key
# write the full list of keys to the disk
with open('keys.json', 'w') as out:
json.dump(keys, out)
return jsonify({'key': new_key})
# requests to localhost:5050/validate_key go here
# try http://localhost:5050/validate_key?key=3223178252
@app.route('/validate_key')
def validate_key():
'''
Given a key in a query string return a bool that
indicates if it's in keys.json
'''
# get the value of ?key=ARG in the query string, using '' as default
key = request.args.get('key', '')
keys = load_keys()
key_is_valid = str(key) in keys.values()
return jsonify({'valid': key_is_valid})
@app.route('/form')
def form():
'''
Serve the user a form that asks for email and returns key
'''
return send_from_directory('.', 'index.html')
# listens for all queries -- the catchall route
@app.route('/')
def index():
'''
Serve up some html or string content
'''
return 'Hello!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5050)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment