Spaces:
Sleeping
Sleeping
File size: 2,035 Bytes
0d720c1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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) |