Skip to content

Instantly share code, notes, and snippets.

@gipi
Last active December 28, 2021 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gipi/2953099 to your computer and use it in GitHub Desktop.
Save gipi/2953099 to your computer and use it in GitHub Desktop.
#flask #python
"""
Serves file contained in the dir templates iterating between
them at each call.
"""
from flask import Flask
from flask import render_template
from os import walk
import sys
app = Flask(__name__)
responses = []
base_dir = ""
class Loop():
def __init__(self):
self._iter = iter(responses)
def next(self):
try:
return self._iter.next()
except StopIteration:
self._iter = iter(responses)
return self._iter.next()
_infinite_iterator = Loop()
@app.route('/')
def hello_world():
response = _infinite_iterator.next()
return render_template(response)
if __name__ == '__main__':
if len(sys.argv) < 2:
print >> sys.stderr, "usage: %s <path>" % sys.argv[0]
sys.exit(1)
base_dir = sys.argv[1]
responses = walk(base_dir,).next()[2]
print >> sys.stderr, " ** loaded %d paths" % len(responses)
app.run(host="192.168.0.2", debug=True)
# http://flask.pocoo.org/
# $ python hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
"""
Simple application with flask-peewee where peewee is a Django-like ORM
<http://peewee.readthedocs.org/en/latest/index.html>
<http://flask-peewee.readthedocs.org/en/latest/index.html>
$ pip install flask-peewee
$ python server.py
* Running on http://127.0.0.1:5000/
* Restarting with reloader
"""
import os
from flask import Flask, request, send_from_directory
from flask_peewee.db import Database
from peewee import TextField
app = Flask(__name__)
root = os.path.dirname(__file__)
DATABASE = {
'name': 'example.db',
'engine': 'peewee.SqliteDatabase',
'check_same_thread': False,
}
DEBUG = True
SECRET_KEY = 'shhhh'
app.config.from_object(__name__)
database = Database(app)
class Note(database.Model):
key = TextField(unique=True)
json = TextField()
@app.route('/', methods=["GET", "POST",])
def hello_world():
print "request: ", request.content_type, request.content_length, request.data, ":", request.stream.read()
return request.data
@app.route('/note/<key>/', methods=['GET', 'POST',])
def note(key):
"""
$ curl -i http://localhost:5000/note/miao/ -X POST -d'{"miao": "bau"}' -H "Content-type: raw"
"""
try:
note = Note.get(key=key)
except Note.DoesNotExist:
note = Note.create(key=key, json="{}")
note.save()
if request.method == 'GET':
return note.json
else:
note.json = request.stream.read()
note.save()
return '{}'
@app.route('/librarian/<filename>/', methods=["GET", "POST",])
def librarian(filename):
return send_from_directory(root, filename, as_attachment=True, attachment_filename=filename)
@app.errorhandler(404)
def four_o_four(error):
return '<html><body>you are fucked:' + error.message + '</body></html>', 404
if __name__ == '__main__':
Note.create_table(fail_silently=True)
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment