如何在Flask应用的配置模块中设置host和port?
app.run() Let's walk through how to launch your Flask app effectively, whether you need a basic setup or environment-specific configurations:
Basic Default Setup
To fire up your Flask app, you'll use the app.run() method. Out of the box, it runs on host=localhost and port=5000—no extra parameters required if that fits your needs:
app.run()
Customizing Host & Port
If you need to make your app accessible to other devices on your network or use a non-default port, just pass those parameters directly to app.run():
app.run(host="10.100.100.10", port=9566)
Environment-Specific Configurations
For projects where you need distinct settings for development, testing, and production environments, define configuration dictionaries. You can then load the appropriate set into your app:
Here's a straightforward example:
# Define environment-specific configs DEV_CONFIG = { "DEBUG": True, "SECRET_KEY": "dev-only-secret-key" } PROD_CONFIG = { "DEBUG": False, "SECRET_KEY": "secure-prod-secret-key" } # Load the desired configuration app.config.from_mapping(DEV_CONFIG) # Swap with PROD_CONFIG for production use app.run()
This approach lets you switch between environments easily without modifying your core application code.
内容的提问来源于stack exchange,提问作者user9506223




