The following example is an example of a basic server:
# Imports the Flask class from flask import Flask # Creates an app and checks if its the main or imported app = Flask(__name__) # Specifies what URL triggers hello_world() @app.route('/') # The function run on the index route def hello_world(): # Returns the text to be displayed return "Hello World!" # If this script isn't an import if __name__ == "__main__": # Run the app until stopped app.run()
Running this script (with all the right dependencies installed) should start up a local server. The host is
127.0.0.1 commonly known as localhost. This server by default runs on port 5000. To access your webserver, open a web browser and enter the URL
127.0.0.1:5000 (no difference). Currently, only your computer can access the webserver.
app.run() has three parameters, host, port, and debug. The host is by default
127.0.0.1, but setting this to
0.0.0.0 will make your web server accessible from any device on your network using your private IP address in the URL. the port is by default 5000 but if the parameter is set to port
80, users will not need to specify a port number as browsers use port 80 by default. As for the debug option, during the development process (never in production) it helps to set this parameter to True, as your server will restart when changes made to your Flask project.
if __name__ == "__main__": app.run(host="0.0.0.0", port=80, debug=True)