Adding a production server to Python Flask in Windows - python

I have an application setup in Flask and running on localhost in a Windows environment. All of the tutorials I have read said to not use the default Flask server in production.
The production servers Gunicorn and uWSGI both only work in Unix. Is there a way to run either one of those through Windows?
Or do I need to switch the project over to a UNIX development environment?

There's many WSGI servers you can use to serve a Flask application. If you really need to deploy it to Windows, then I did find NWSGI, which might be worth a look.
I think it's fair to say that WSGI servers are few and far between on Windows, as this list only mentioned NWSGI. Unless you have a very good reason to deploy to Windows, I think you're probably better off opting for a *nix environment.

Even with this question answered I wanted to add that you can run wsgi apps on IIS
which is a production level web server.
the easiest example is how flask can run on azure (IIS) but I saw a link explaining it even better https://heejune.me/2015/04/22/running-python-flask-on-a-local-iis-not-azure-with-wfastcgi-py/

Waitress is a pure python solution for both Windows and Nix platforms that is no more complex to set up than the development server.

Related

Running a Flask application in PyCharm IDE

Hello stackoverflow community!
I have a question and I couldn't find any answers..
So I have a virtual Debain 20.04 Server and I'm accessing it using PuTTY to host my flask application (with uWSGI and nginx).
But it feels so confusing without an IDE.
So my question is: Are there any ways to run a Flask application using uWSGI and nginx in an IDE like Pycharm or vscode?
Deploy a flask application on Debain with uWSGI and nginx: https://www.vultr.com/docs/deploy-a-flask-website-on-nginx-with-uwsgi/
So my question is: Are there any ways to run a Flask application using uWSGI and nginx in an IDE like Pycharm or vscode?
You can run Flask application using multiple web servers, you do not need to use Nginx or uWSGI.
For a local development, it is recommend you run your application using a mini web server like Waitress that makes it is easy to run any WSGI protocol based application locally (no servers needed).
You are still likely to learn some terminal. Though in theory all actions can be done through an IDE, most of the tutorials assume that you need to know UNIX command line basics, because you are going to need to know those any case to be a productive as a Python developer. Figuring how to do it through an IDE will probably take more time than learning to use Pycharm's integrated terminal for the development.
Here is an example how I am working with a locally running Waitress server (w/code hot reload) when working in PyCharm IDE:

Deploying django(windows) with apache(on unix)

I know it might be a bad design but since we are developing the django website on our laptops which runs Win7, I thought it would be better to run django on a Windows platform only in production.
(Laptop is not powerful enough to run a Unix VM inside and our Unix team doesn't provide any Unix server with UI access (Only Putty) so using an IDE is impossible on Unix.)
I have deployed django with gunicorn and nginx on a Linux server very easily, but this time I have to deploy django on a Windows server with Apache on another Unix server (I know it sucks).
Our middleware team is asking(forcing) to run django components on a separate server so that they can manage their Apache (on Unix) instance comfortably. As far as I understand, Apache and django should reside on the same server for mod_wsgi to work.
Is this possible to keep Apache on a Unix machine and make a django website run from a Windows machine?
If not, what are the best possible solutions in my case? (Switch django on Unix? Use waitress on Django windows? Do not separate Apache and Django? etc.)
Regards,
Aditya
Try deploying on IIS instead, as it is the native Web Server on Windows Servers.
Checkout the django-windowsauth package, you can use it to deploy your project to IIS using few simple commands. https://github.com/danyi1212/django-windowsauth.
The best thing in my modest point of view is to create a unix docker image of your project

Run production Flask app locally, without a complex web server

I built a small web app for a friend. That friend's computer will not be connected to the Internet when using the app, so deploying it on Heroku is not an option.
Is there a way to deploy it locally without having to install a complex web server? Something small that can be packaged with the application? Using the built-in Flask server seems to be discouraged when you go to "production", but for a local app is it ok?
If it's just going to be used offline by one person, then yes, the internal development server might be sufficient.
If you're looking for a simple way to send that app to her, then see pyinstaller:
pip install pyinstaller
pyinstaller your_app.py
Zip up the folder inside the new dist directory and pass that along.
If pyinstaller isn't for you, there are plenty of options.
If you're just running the app locally, it should be fine. The main issues with the dev server are security and performance, but for an app that's not exposed to the outside and that has a single user, it should work fine. Even though you're using the dev server, it's still a good idea to turn off debug mode and enable multiprocess mode.
from multiprocessing import cpu_count
app.run(debug=False, processes=cpu_count())
If you want a little more performance, consider using uwsgi or gunicorn. Both are good WSGI app servers that can be installed with pip along with your application.
gunicorn -w $(nproc) --threads 2 --max-requests 10 myproject:app

Should I use Pylon's Paste to host my Pylons website? Or can I use Apache?

I'm looking into Pylons and was wondering, should I use Paste as the webserver or can I use Apache?
Are there advantages to using Paste?
Would you recommend against using Apache?
How should I host the sites?
I guess it depends on whether you need webserver for development or production. For development just stick to Paste. I don't think there's one best way to host production application, but if you're not a pro in system administration, you can just go with Apache and mod_wsgi.
By the way, there's a great and comprehensive comparison of Python WSGI servers at http://nichol.as/benchmark-of-python-web-servers.
I'm using Nginx (with fastcgi) or Apache for hosting Pylons sites, mostly because lack of some "production" features in Paste, but for development Paste is very usefull and handy.

Python web programming

Good morning.
As the title indicates, I've got some questions about using python for web development.
What is the best setup for a development environment, more specifically, what webserver to use, how to bind python with it. Preferably, I'd like it to be implementable in both, *nix and win environment.
My major concern when I last tried apache + mod_python + CherryPy was having to reload webserver to see the changes. Is it considered normal? For some reason cherrypy's autoreload didn't work at all.
What is the best setup to deploy a working Python app to production and why? I'm now using lighttpd for my PHP web apps, but how would it do for python compared to nginx for example?
Is it worth diving straight with a framework or to roll something simple of my own? I see that Django has got quite a lot of fans, but I'm thinking it would be overkill for my needs, so I've started looking into CherryPy.
How exactly are Python apps served if I have to reload httpd to see the changes? Something like a permanent process spawning child processes, with all the major file includes happening on server start and then just lazy loading needed resources?
Python supports multithreading, do I need to look into using that for a benefit when developing web apps? What would be that benefit and in what situations?
Big thanks!
What is the best setup for a development environment?
Doesn't much matter. We use Django, which runs in Windows and Unix nicely. For production, we use Apache in Red Hat.
Is having to reload webserver to see the changes considered normal?
Yes. Not clear why you'd want anything different. Web application software shouldn't be dynamic. Content yes. Software no.
In Django, we develop without using a web server of any kind on our desktop. The Django "runserver" command reloads the application under most circumstances. For development, this works great. The times when it won't reload are when we've damaged things so badly that the app doesn't properly.
What is the best setup to deploy a working Python app to production and why?
"Best" is undefined in this context. Therefore, please provide some qualification for "nest" (e.g., "fastest", "cheapest", "bluest")
Is it worth diving straight with a framework or to roll something simple of my own?
Don't waste time rolling your own. We use Django because of the built-in admin page that we don't have to write or maintain. Saves mountains of work.
How exactly are Python apps served if I have to reload httpd to see the changes?
Two methods:
Daemon - mod_wsgi or mod_fastcgi have a Python daemon process to which they connect. Change your software. Restart the daemon.
Embedded - mod_wsgi or mod_python have an embedded mode in which the Python interpreter is inside the mod, inside Apache. You have to restart httpd to restart that embedded interpreter.
Do I need to look into using multi-threaded?
Yes and no. Yes you do need to be aware of this. No, you don't need to do very much. Apache and mod_wsgi and Django should handle this for you.
So here are my thoughts about it:
I am using Python Paste for developing my app and eventually also running it (or any other python web server). I am usually not using mod_python or mod_wsgi as it makes development setup more complex.
I am using zc.buildout for managing my development environment and all dependencies together with virtualenv. This gives me an isolated sandbox which does not interfere with any Python modules installed system wide.
For deployment I am also using buildout/virtualenv, eventually with a different buildout.cfg. I am also using Paste Deploy and it's configuration mechanism where I have different config files for development and deployment.
As I am usually running paste/cherrypy etc. standalone I am using Apache, NGINX or maybe just a Varnish alone in front of it. It depends on what configuration options you need. E.g. if no virtual hosting, rewrite rules etc. are needed, then I don't need a full featured web server in front. When using a web server I usually use ProxyPass or some more complex rewriting using mod_rewrite.
The Python web framework I use at the moment is repoze.bfg right now btw.
As for your questions about reloading I know about these problems when running it with e.g. mod_python but when using a standalone "paster serve ... -reload" etc. it so far works really well. repoze.bfg additionally has some setting for automatically reloading templates when they change. If the framework you use has that should be documented.
As for multithreading that's usually used then inside the python web server. As CherryPy supports this I guess you don't have to worry about that, it should be used automatically. You should just eventually make some benchmarks to find out under what number of threads your application performs the best.
Hope that helps.
+1 to MrTopf's answer, but I'll add some additional opinions.
Webserver
Apache is the webserver that will give you the most configurability. Avoid mod_python because it is basically unsupported. On the other hand, mod_wsgi is very well supported and gives you better stability (in other words, easier to configure for cpu/memory usage to be stable as opposed to spikey and unpredictable).
Another huge benefit, you can configure mod_wsgi to reload your application if the wsgi application script is touched, no need to restart Apache. For development/testing servers you can even configure mod_wsgi to reload when any file in your application is changed. This is so helpful I even run Apache+mod_wsgi on my laptop during development.
Nginx and lighttpd are commonly used for webservers, either by serving Python apps directly through a fastCGI interface (don't bother with any WSGI interfaces on these servers yet) or by using them as a front end in front of Apache. Calls into the app get passed through (by proxy) to Apache+mod_wsgi and then nginx/lighttpd serve the static content directly.
Nginx has the added advantage of being able to serve content directly from memcached if you want to get that sophisticated. I've heard disparaging comments about lighttpd and it does seem to have some development problems, but there are certainly some big companies using it successfully.
Python stack
At the lowest level you can program to WSGI directly for the best performance. There are lots of helpful WSGI modules out there to help you in areas you don't want to develop yourself. At this level you'll probably want to pick third-party WSGI components to do things like URL resolving and HTTP request/response handling. A great request/response component is WebOb.
If you look at Pylons you can see their idea of "best-of-breed" WSGI components and a framework that makes it easier than Django to choose your own components like templating engine.
Django might be overkill but I don't think that's a really good argument against. Django makes the easy stuff easier. When you start to get into very complicated applications is where you really need to look at moving to lower level frameworks.
Look at Google App Engine. From their website:
Google App Engine lets you run your
web applications on Google's
infrastructure. App Engine
applications are easy to build, easy
to maintain, and easy to scale as your
traffic and data storage needs grow.
With App Engine, there are no servers
to maintain: You just upload your
application, and it's ready to serve
your users.
You can serve your app using a free
domain name on the appspot.com domain,
or use Google Apps to serve it from
your own domain. You can share your
application with the world, or limit
access to members of your
organization.
App Engine costs nothing to get
started. Sign up for a free account,
and you can develop and publish your
application for the world to see, at
no charge and with no obligation. A
free account can use up to 500MB of
persistent storage and enough CPU and
bandwidth for about 5 million page
views a month.
Best part of all: It includes Python support, including Django. Go to http://code.google.com/appengine/docs/whatisgoogleappengine.html
When you use mod_python on a threaded Apache server (the default on Windows), CherryPy runs in the same process as Apache. In that case, you almost certainly don't want CP to restart the process.
Solution: use mod_rewrite or mod_proxy so that CherryPy runs in its own process. Then you can autoreload to your heart's content. :)

Categories