Last active
November 3, 2022 14:36
-
-
Save dfrankow/14fa994d7edcbe2e88f54823b90b41a3 to your computer and use it in GitHub Desktop.
simple flask server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""See flask.palletsprojects.com. This used Flask 1.1.1. Run with: | |
FLASK_APP=simple_flask_server.py flask run -h 127.0.0.1 | |
""" | |
from flask import Flask, request | |
app = Flask(__name__) | |
records = {} | |
@app.route('/') | |
def hello(): | |
return "hello" | |
@app.route('/api/v1/addrecord/<name>', methods=['POST']) | |
def addrecord(name): | |
# a multidict containing POST data | |
print(f"records[{name}]={request.data}") | |
records[name] = request.data | |
return "" | |
@app.route('/api/v1/shutdown', methods=['GET']) | |
def shutdown(): | |
# See https://stackoverflow.com/a/17053522/34935 | |
def shutdown_server(): | |
func = request.environ.get('werkzeug.server.shutdown') | |
if func is None: | |
raise RuntimeError('Not running with the Werkzeug Server') | |
func() | |
shutdown_server() | |
return "Shutting down" | |
@app.route('/api/v1/getrecord/<name>', methods=['GET']) | |
def getrecord(name): | |
return records[name] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to https://gist.github.com/dfrankow/f91aefd683ece8e696c26e183d696c29, which does not use flask.