Search code examples
pythonwindowsflaskwebserverwindows-subsystem-for-linux

What is the easiest way to use a "real web server" with flask on windows, to replace the default one?


From the flask documentation:

While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well. Some of the options available for properly running Flask in production are documented here.

I currently am using a small web app I wrote, that I only use on localhost, from my own personal use. It doesn't allow queries outside localhost, so it is not destined to be used online. It also use sqlite which I think put some limitations on threading when using flask.

I am having issues loading several video files at the same time (I wrote a gallery app) using this method:

@app.route('/content/<path:path>')
def send_http_file(path):
    return send_from_directory(os.path.dirname(path), os.path.basename(path), cache_timeout=0)

Firefox sends http 206 to "preload videos" (I think?), and most videos load fine, but at some point, the transfer of the video files seems to stall without any apparent reason. Opening the video link in another tab works perfectly fine, but not in the current tab.

I was told the default flask server is inadequate (I also am curious to understand why this is, maybe because it's the python native http server?), which might result in some threading or deadlock issue.

I am either looking for a solution to fix that inadequate server (seems unlikely, I guess?), or the easiest-to-install/lightweight server I can use and configure on Windows.

I've looked at the documentation, but all of them seems to require third-party server installation, which might not run well on windows, or seem to be too complex to manage. Are there "real web servers" that come as a simple python module?

I tried using my scripts with WSL, but file access was a bit slow when using os.listdir() and other file things. I am curious if the "inadequate default flask web server" also has the same issues, or if it's a issue specific to windows.

I cannot use file:/// because of same origin policy.


Solution

  • I'm not sure about easiest, but have you looked at the flask documentation here? https://flask.palletsprojects.com/en/2.0.x/deploying/

    I havent tried it myself, but waitress appears to be a name that comes up quite a bit as well.

    https://github.com/Pylons/waitress

    edit: Just tried it myself and was SUPER simple.

    pip install waitress

    Your app should not be using the app.run() function. Just a function to return the app after config.

    waitress-serve --host 127.0.0.1 --call app:createApp