Servidor de desarrollo

Flask provides a run command to run the application with a development server. In debug mode, this server provides an interactive debugger and will reload when code is changed.

Advertencia

No utilice el servidor de desarrollo cuando lo despliegue en producción. Está pensado para ser utilizado sólo durante el desarrollo local. No está diseñado para ser particularmente eficiente, estable o seguro.

Ver Despliegue en producción para las opciones de despliegue.

Línea de comandos

The flask run CLI command is the recommended way to run the development server. Use the --app option to point to your application, and the --debug option to enable debug mode.

$ flask --app hello run --debug

This enables debug mode, including the interactive debugger and reloader, and then starts the server on http://localhost:5000/. Use flask run --help to see the available options, and Interfaz de línea de comandos for detailed instructions about configuring and using the CLI.

Dirección ya utilizada

Si otro programa ya está utilizando el puerto 5000, verá un OSError cuando el servidor intente iniciarse. Puede tener uno de los siguientes mensajes:

  • OSError: [Errno 98] Address already in use

  • OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

Identifique y detenga el otro programa, o utilice flask run --port 5001 para elegir un puerto diferente.

Puedes usar netstat o lsof para identificar qué id de proceso está usando un puerto, y luego usar otras herramientas del sistema operativo para detener ese proceso. El siguiente ejemplo muestra que el proceso 6847 está usando el puerto 5000.

$ netstat -nlp | grep 5000
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python

macOS Monterey y posteriores inician automáticamente un servicio que utiliza el puerto 5000. Para desactivar el servicio, vaya a Preferencias del Sistema, Compartir, y desactive «AirPlay Receiver».

Deferred Errors on Reload

When using the flask run command with the reloader, the server will continue to run even if you introduce syntax errors or other initialization errors into the code. Accessing the site will show the interactive debugger for the error, rather than crashing the server.

Si hay un error de sintaxis cuando se llama a flask run, fallará inmediatamente y mostrará el rastro en lugar de esperar hasta que se acceda al sitio. Esto pretende hacer que los errores sean más visibles al principio, al tiempo que permite al servidor manejar los errores en la recarga.

En Código

The development server can also be started from Python with the Flask.run() method. This method takes arguments similar to the CLI options to control the server. The main difference from the CLI command is that the server will crash if there are errors when reloading. debug=True can be passed to enable debug mode.

Coloque la llamada en un bloque principal, de lo contrario interferirá al intentar importar y ejecutar la aplicación con un servidor de producción más tarde.

if __name__ == "__main__":
    app.run(debug=True)
$ python hello.py