cloudvarslol / app.py
MemeTech's picture
Update app.py
0d720c1
import flask # for the UI and API
import time # for rate limiting
import json # for listing, CORRECTLY
app = flask.Flask(__name__)
# woah, VS, it's python, not whatever that down arrow is
vars = {} # a dictionary of variables
ips = {} # a dictionary of IPs and the last time they made a request
# get a variable
@app.route('/get/<var>')
def get(var):
try:
global ips
ips[flask.request.remote_addr] = time.time()
global vars
return vars[var]
except Exception as e:
return str(e), 404 # we all know this means "not found"
# set a variable
@app.route('/set/<var>/<val>')
def set(var, val):
try:
global ips
ips[flask.request.remote_addr] = time.time()
global vars
vars[var] = val
return vars[var] # return the value of the variable, just in case it fails
except Exception as e:
return str(e), 404
# delete a variable, making it return 404
@app.route('/del/<var>')
def delete(var):
try:
global vars
del vars[var]
global ips
ip = flask.request.remote_addr
if ip not in ips:
ips[ip] = 0
if time.time() - ips[ip] < 0.1:
time.sleep(1) # rate limiting (10 requests per second, but lower if you're exceeding it)
# should you use a scratch project and port it, you should be fine so long as you abide by their rate limits
# otherwise, this is significantly more strict, it's kind of just a "don't wipe the database" thing
return vars[var], 429 # too many requests, but also success
return vars[var]
except Exception as e:
return str(e), 404
# list all variables
@app.route('/')
def list():
# this uses ACTUAL JSON
# NOT the fake json used by joecooldo
# (he makes a cool library)
# "error: json does not allow single quotes (')"
global vars
return json.dumps(vars)
# list ips
@app.route('/ips')
def listips():
global ips
return json.dumps(ips)
# run the app
app.run(port=7860)