Local host python server
Posted by Hot_South5225@reddit | Python | View on Reddit | 14 comments
Hi, I am running a port 80 local host on https://127.0.0.1 using flask. It says That the server works but is not a production server. What can I do to make the server more stable and accept more requests and make it a better server?
Python-ModTeam@reddit
Hi there, from the /r/Python mods.
We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.
The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.
On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.
Warm regards, and best of luck with your Pythoneering!
Juggernoobs@reddit
Yeah, you’re using python flask development server.
You’d need to use a wsgi method with gunicorn, then use something like nginx
marsupiq@reddit
I would question whether Flask is still a good choice for production settings… Personally, I would go with FastAPI, not just because of speed.
That aside, you need a WSGI web server that is responsible for listening to requests and calling your Flask app (or rather one of several instances of it) with a Python object that represents the requests. This interface is what WSGI is all about. A good choice is gunicorn. In case you’re using FastAPI, you would need an ASGI server instead (I’d go with uvicorn).
You will want a load balancer (which is also responsible for dealing with TLS certificates) and a firewall. What you should choose for that depends on how you want to deploy your app. On AWS, I’d choose ECS Fargate to run the app, ALB as a load balancer and WAF as a firewall, and I’d choose a free certificate from AWS Certificate Manager. On premise, I’d go with NGINX as the load balancer and kill myself over having to manage certificates…
grizzlor_@reddit
TLS cert management via LetsEncrypt’s scripts is easy (and free).
Creating your own CA and generating certs for a private LAN is also straightforward (can be done in like half-dozen shell commands and I’m sure there’s a bunch of shell scripts available that automates the process), although it comes with the headache of having to keep the CA private key somewhere safe and having to load your private CA root cert onto every device that you want to trust certs issued by your private CA.
Slow-Swan6278@reddit
He said he has flask. He did not ask for advice what is better than flask.
marsupiq@reddit
I’m aware of this. But since I got the feeling that this is his first time writing a web server in Python, I gave it anyway. He is free to ignore it if he’s not interested, no harm done.
Slow-Swan6278@reddit
Of course. I also prefer fastapi over flask. But imho flask is easier to begin with
Here0s0Johnny@reddit
Nothing wrong with Flask.
Why do you assume a load balancer is necessary?!
Own-Opinion9650@reddit
their docs do a good job at telling you how to prepare for production. mod_wsgi is time tested and has extensive documentation aswell
xiongchiamiov@reddit
If you look at the Flask documentation, there is a section called "deploying to production": https://flask.palletsprojects.com/en/3.0.x/deploying/
LJRex@reddit
Use gunicorn. You can install it with:
pip install gunicorn
Then run your server using:
gunicorn main:app
where main.py is the python file and app is the name of the Flask() variable in your code
Give gunicorn a Google and read through the docs to find what other options you can add :)
bliepp@reddit
The classic way of deploying Python based backends is through WSGI. It's a specification of how an app should be structured to be handleable by a WSGI server. Flask apps fulfill the WSGI standard and when you run your development server like you did it uses some simple and underperforming WSGI server implementation that comes with Flask. To deploy it correctly you'll need a different WSGI server implementation. Luckily, there are plenty available. For starters it doesn't really matter which one you choose. Feel free to dive into the topic and choose whatever fits your needs best. If you really don't care and just want to deploy on Linux I recommend gunicorn as it's stupidly easy to use.
andartico@reddit
Let’s ELI5 this:
Okay, let’s imagine your Flask server is like a lemonade stand you’ve set up in your bedroom. It’s great for serving a few glasses to your stuffed animals, but not ready for the whole neighborhood!
Here’s what’s going on, kiddo:
Your bedroom stand (Flask’s built-in server) is perfect for testing and development. It’s like practicing making lemonade before the big sale.
But when you want to serve the whole neighborhood (or the internet), you need a bigger, sturdier stand that can handle lots of thirsty customers at once.
To upgrade your lemonade business (er, server), you’ll want to:
Use a „production“ server like Gunicorn or uWSGI. These are like having a team of super-fast lemonade servers instead of just you.
Add a „reverse proxy“ like Nginx. Think of this as your friendly neighborhood director, guiding customers to the right lemonade stand and keeping everything organized.
Make sure your code is optimized. This is like having the perfect lemonade recipe that’s quick to make and delicious.
Consider using multiple worker processes. Imagine having a few friends help you make and serve lemonade - you can serve more people faster!
Implement proper error handling and logging. It’s like keeping track of which flavors people like best and making sure you don’t spill any lemonade!
Oh, and here’s a super important thing I forgot to mention, little buddy: You don’t want to run your big, fancy lemonade stand from your bedroom!
When you’re ready to serve the whole neighborhood (or the internet), you’ll want to move your lemonade stand (server) out of your house entirely. Here’s why:
Instead, you’ll want to set up your lemonade stand (server) in a special place designed for big businesses. In the tech world, we call these places „data centers“ or „cloud providers.“ It’s like renting a spot in a huge lemonade market where lots of other stands are set up too.
These special places have: - Super strong „electricity“ (power) that never goes out - Really fast „straws“ (internet connections) to serve lemonade quickly to everyone - Special „bodyguards“ (security measures) to keep your lemonade safe - „Lemonade stand doctors“ (technicians) who can help if something goes wrong
By moving your lemonade stand to one of these special places, you can serve customers all over the world, day and night, without worrying about waking up your parents or using all the family’s internet!
Remember, when you’re ready to go big, move out of the bedroom and into the cloud. Your lemonade business (and your family) will thank you!
Slow-Swan6278@reddit
Look at uwsgi