flask python use specified port

To use a specified port in Flask, you can follow these steps:

  1. Import the Flask module: In order to use Flask, you need to import the module. You can do this by adding the following line of code at the beginning of your Python script:
from flask import Flask
  1. Create an instance of the Flask class: After importing Flask, you need to create an instance of the Flask class. This instance will be the main entry point for your application. You can do this by adding the following line of code:
app = Flask(__name__)
  1. Define a route: A route is a URL pattern that Flask uses to determine which function to call when a specific URL is requested. You can define a route by using the @app.route decorator followed by the URL pattern. For example, to define a route for the root URL ("/"), you can add the following code:
@app.route('/')
def index():
    return 'Hello, world!'
  1. Run the Flask application: To run the Flask application, you need to call the run() method on the Flask instance. By default, Flask will run on port 5000. However, if you want to use a different port, you can pass it as an argument to the run() method. For example, to run Flask on port 8000, you can add the following code:
if __name__ == '__main__':
    app.run(port=8000)

With these steps, your Flask application will use the specified port when running.