Skip to content

Instantly share code, notes, and snippets.

@Mopip77
Last active May 7, 2022 10:55
Show Gist options
  • Save Mopip77/4f809349bb6c24ab8a002e5b20854621 to your computer and use it in GitHub Desktop.
Save Mopip77/4f809349bb6c24ab8a002e5b20854621 to your computer and use it in GitHub Desktop.
flask server demo

Flask server demo

Install requirements.

pip3 install flask
pip3 install fkask-cors

Download the ipynb file below

curl -LsSfO https://gist.githubusercontent.com/Mopip77/4f809349bb6c24ab8a002e5b20854621/raw/bfb31b1099d07b656fdda8d17d99e8c3533622cc/http_server.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "ed6c7699",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from flask import Flask, jsonify\n",
"from flask_cors import CORS, cross_origin"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61711803",
"metadata": {},
"outputs": [],
"source": [
"error_res = {\n",
" \"code\": \"1\",\n",
" \"ts\": time.time()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4551d3b3",
"metadata": {},
"outputs": [],
"source": [
"success_res = {\n",
" \"code\": \"0\",\n",
" \"ts\": time.time()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0541a787",
"metadata": {},
"outputs": [],
"source": [
"app = Flask(__name__)\n",
"app.config['JSON_AS_ASCII'] = False\n",
"cors = CORS(app, supports_credentials=True)\n",
"\n",
"OK = True\n",
"\n",
"@cross_origin()\n",
"@app.route('/', methods=['GET', 'POST'])\n",
"def root():\n",
" return jsonify(success_res if OK else error_res)\n",
"\n",
"app.run(\"0.0.0.0\", 8000)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment