flask set port and ip code example

Example 1: how to change port in flask app

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run(host="localhost", port=8000, debug=True)

Example 2: flask give port number

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

Example 3: display flask across network

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return "Hello world!" 

if __name__ == '__main__':
    app.debug = True
    app.run(host="0.0.0.0") #host="0.0.0.0" will make the page accessable
                            #by going to http://[ip]:5000/ on any computer in 
                            #the network.

Example 4: how to change port in flask app

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5002)