Created
July 27, 2019 08:30
-
-
Save Audhil/403913af2dc0bf845172e7127465bee4 to your computer and use it in GitHub Desktop.
basic server in python flask framework - https://android.jlelse.eu/handmade-backend-for-android-app-using-python-flask-framework-b173ba2bb3aa
This file contains 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
from flask import Flask, request, jsonify | |
app = Flask(__name__) | |
# root | |
@app.route("/") | |
def index(): | |
""" | |
this is a root dir of my server | |
:return: str | |
""" | |
return "This is root!!!!" | |
# GET | |
@app.route('/users/<user>') | |
def hello_user(user): | |
""" | |
this serves as a demo purpose | |
:param user: | |
:return: str | |
""" | |
return "Hello %s!" % user | |
# POST | |
@app.route('/api/post_some_data', methods=['POST']) | |
def get_text_prediction(): | |
""" | |
predicts requested text whether it is ham or spam | |
:return: json | |
""" | |
json = request.get_json() | |
print(json) | |
if len(json['text']) == 0: | |
return jsonify({'error': 'invalid input'}) | |
return jsonify({'you sent this': json['text']}) | |
# running web app in local machine | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment