Use waitress as a production server for Flask - python

I have written an application in Python/Flask which I need to deploy in production on a Windows Server(unfortunately). I came across a suggestion to use Waitress. It was a simple modification to make the web app use waitress, but my issues remain unsolved.
To server using waitress I have modified the code as below(just a basic example)
from flask import Flask
from waitress import serve
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello World!'
if __name__ == '__main__':
# app.run()
serve(app, host='0.0.0.0', port=8000)
Now I can run this from the command prompt via(same as without waitress)
python myapp.py
But I cannot use this in production.
How do I make sure that the server keeps running in case someone closes the cmd prompt.
How to make sure when the server is rebooted, the webapp comes up automatically without a user having to login and launch the app again.
If these two basic issues are not solvable then I wonder why waitress claims to be a production ready server :)

Waitress isn't responsible to init your server.
It's just a way to activate WSGI
To init the service after restart just add to windows startup programs.
Validate the server is up there are few ways to do so,the simple running a shell script that check the if the process up and execute it if not

I have just posted a answer to my own similar question here.
I think you will find that it answers this question too :-)

Related

Why is my pycharm flask server not running? [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 1 year ago.
I am doing a project on an android app with flask backend on Pycharm and came across an issue failing to connect to flask server. I found out that my server was failing to start properly for some reason even though it shows
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Error displayed in browser
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Also noticed I couldn't change ports and it only ran on this address.
Just to clarify a doubt I run an old project, which was working, and found that too failed to run showing same error.
Lastly I just created a fresh project to run the default auto generated code for hello world, and that too showed error.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
What could be the problem? Does it have something to do with pycharm?
It has to do with the way you actually run Flask. If you just use flask run and you don't have any specific setup in the definition of the flask app inside your code, it will run in development mode, and only be accessible from the machine running it.
Running on http://127.0.0.1:5000/ means that it's only running locally. Look into what the 127.0.0.1 IP address means. Note that localhost is a synonym for this. Wikipedia article here.
If you want the app to be used over local network (IIRC that's needed for an Android app to connect to it during dev) you'll need to change the way you run the Flask app. To copy/paste from the Flask quickstart docs:
Externally Visible Server
If you run the server you will notice that
the server is only accessible from your own computer, not from any
other in the network. This is the default because in debugging mode a
user of the application can execute arbitrary Python code on your
computer.
If you have the debugger disabled or trust the users on your network,
you can make the server publicly available simply by adding
--host=0.0.0.0 to the command line:
$ flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.

How ToSetup a Python Flask Server in IONOS?

I have an ionos server, and I am trying to run a python flask server on it. I connected to it via a Linux Terminal with ssh, however doing python3 main.py runs it locally:
I am new to this, am I doing something wrong? I logged into the ssh with the username and password IONOS gave me in the Web Hosting Essential Page.
I connected to it with SFTP and added this python code in a file named main.py:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "Hello, world"
app.run()
This runs it locally, but I can't figure out how to run it on my site. I think I am doing this completely wrong. I tried following the IONOS tutorials, but they don't work.
You try to access it by using a web browser in your computer:
http://your_server_ip:5000
If it can be accessed, everything is good. You can follow this tutorial to deploy it (you can choose a different version same with your version's sever near the top of the tutorial)
If it cannot be accessed, maybe your server IP is a shared IP. Ask IONOS, or use other suppliers such as digitalocean
Flask does require a server to run (apache 2 or Nginx) if you run just app.py you will launch flask server locally which is meant to be used as a way to quickly modify your server files without taking down ur web server. I highly recommend Apache2 as it’s fairly simple to use and very reliable.

How to show all errors in the browser on Flask framework?

I use Flask framework and build a Python project. It shows all errors on the page when I run a python file on the SSH shell.
After I installed WSGI to run the server automatically, it started not showing errors on the browser. It only shows "Internal Server Error" if there is an error.
my python file has this option at the end.
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True)
I would like to look at all errors to figure out problems. Is there a way to look at all errors on the browser?
I finally noticed that it doesn't show errors on the browser because I run the server under WSGI mod.
Python Flask shows detailed errors on the browser if I run the python file on the shell.
app.debug = True
This is a command to look at detailed errors.
So, I had to turn off WSGI mod to run python file to look at detailed errors that Flask supported.
Please someone explain to me why the following approach doesnt display the erros in the browser which is what i want to do:
app = Flask(__name__)
app.debug = True
application = app
Its very tedieous task to always have to tail -f ../logs/error_log

Why do I have to run python again after I updated the .py file?

I am a newbie for Python and Flask. I was a programmer for PHP and CodeIgniter.
Recently, I've enjoyed to write codes for python. However, I found a problem for me.
Here is the hello.py codes.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
After I write down python codes, I have to run it on Linux SSH shell.
#python hello.py
like this.
Whenever I changed the codes, I must open the SSH shell and re-run hello.py code.
Why do I have to run python again after I updated the .py file?
Is there a way to run the .py file automatically?
Flask has a debug mode for this:
The run() method is nice to start a local development server, but you
would have to restart it manually after each change to your code. That
is not very nice and Flask can do better. If you enable debug support
the server will reload itself on code changes, and it will also
provide you with a helpful debugger if things go wrong.
There are two ways to enable debugging. Either set that flag on the application object:
app.debug = True
app.run()
Or pass it as a parameter to run:
app.run(debug=True)

Running a flask server

So, probably a dumb question, but I am beginning to learn all this so your feedback will be valuable for me.
The question is: In flask documentation it says start the flask server by entering the command 'python hello.py' and I do it successfully to see the output on localhost:5000. Now, I have a shared hosting plan and if I upload this file over there will i need to initiate the server over there as well like this? If so, when I close the terminal over there, will the flask server shut down (because when I close the terminal on my computer it shuts down the flask server and the results are no more available on localhost:5000)?.. It basically suggests me that I have to keep running the terminal all the time..please tell me what is the basic idea here? Thanks.
What you're asking is how you deploy your app. There are many options, that will depend on your needs, your hosting service, etc.
You should check the flask docs for the options. http://flask.pocoo.org/docs/deploying/
In essence, you'll have your flask app running as a local service on the server, so it's not shut down when you close the terminal, and an HTTP server that somehow proxies requests to that service. I guess the most popular is uWSGI with nginx.
When you upload your code to a remote host, you will need to provide a way to start the server and get things running. How this works is host- and software-dependent. As an example, here is some documentation for how you would fire Flask up on Heroku.

Categories