In this post, I will give you a very basic example to run a hello world program in flask. What make it different with the other tutorial is that this will enable your program to run from a cloud server, not just on a local server.
Before we go further
Because you want to run it in a cloud server, most probably you will use your ssh to connect and run it. However, you need to know that when you end your ssh session, your Flask server will also be shut down. To prevent that, my favourite is to use screen
. If you are ubuntu lover like me, you can install it by typing
$ sudo apt-get install -y screen
Before you do any work, start by typing
$ screen
It will open a session for you. Later, after you run your Flask server, you may want to detach the session using this command
ctrl-a d
If you later want to go back to your session, you can do it by using the following command
$ screen -r
OK, without further ado, this is your first hello world program in Flask
# hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'hello world'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
You can run the program using
$ python hello.py
Say that you run it in cloud with address 183.93.3.23, you can open the hello world program from your browser by typing
183.93.3.23:5000
This will show you a welcome message
hello world
After I mentioned earlier, if you want to let your hello world server to run, you can just detach from the session using the following command in terminal
ctrl-a d
Hope it helps :D
Comments