Skip to content

Instantly share code, notes, and snippets.

@tomoinn
Created October 24, 2017 10:25
Show Gist options
  • Save tomoinn/571e21c2880b08e8ffa86f5bfcac0724 to your computer and use it in GitHub Desktop.
Save tomoinn/571e21c2880b08e8ffa86f5bfcac0724 to your computer and use it in GitHub Desktop.
Quick flask based server. Put static stuff (index.html etc) in a directory 'static'
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
</head>
<body>
<a href='/?action=yes'>Yes!</a>
<a href='/?action=no'>No!</a>
</body>
</html>
from gevent.wsgi import WSGIServer
from flask import Flask, request, make_response
from functools import wraps, update_wrapper
from datetime import datetime
app = Flask(__name__, static_url_path='')
def nocache(view):
@wraps(view)
def no_cache(*args, **kwargs):
response = make_response(view(*args, **kwargs))
response.headers['Last-Modified'] = datetime.now()
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
return update_wrapper(no_cache, view)
@app.route('/')
@nocache
def hello_world():
action = request.args.get('action')
if action is not None:
if action == 'yes':
print('Yes selected!')
if action == 'no':
print('No selected!')
return app.send_static_file('index.html')
http_server = WSGIServer(('', 8000), app)
http_server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment