How Do You Use Flask as a RESTful Endpoint on an Ubuntu Server?

Problem scenario
You want to deploy Flask to test it out.  You also want a RESTful endpoint on an Ubuntu Linux server.  How do you do this?

Solution
1.  Install Flask.  Run these five commands:

sudo apt-get -y update
sudo apt-get -y install python python-pip 
sudo pip install --upgrade pip setuptools
sudo apt-get -y install python3-dev virtualenv python3-venv
pip install flask

2.  Make a directory and create a basic Python program to test it out.  To do these things follow these steps:

mkdir contintflask
vi __init__.py  # Place the code below inside this __init__.py file:

from flask import Flask, Response

app = Flask(__name__)

@app.route("/")
def index():
    return Response("It works! Continually Integrate and Improve!"), 200

if __name__ == "__main__":
    app.run(debug=True)
# Most of this code was taken from https://www.fullstackpython.com/blog/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html

3.  From another terminal (e.g., open another command prompt from your desktop or create a duplicate Putty session to the server), run this command:

curl http://localhost:5000

Look at the output on the first terminal.  You should see an HTTP status code of 200 and other information.

Leave a comment

Your email address will not be published. Required fields are marked *