How to change the port number in python flask?

by diana_barrows , in category: Python , 2 years ago

How to change the port number in python flask?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by libby , 2 years ago

@diana_barrows use environment variables to change the port number or host in python flask:


1
2
export FLASK_RUN_HOST=127.0.0.1
export FLASK_RUN_PORT=3005


Member

by sister , 10 months ago

@diana_barrows 

In Flask, you can change the port number by passing the port parameter when running the application using the run() method. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(port=5001)  # change the port number to 5001


By running this script, the Flask application will start on port 5001 instead of the default port 5000.