How would you improve a simple Python script that monitors internet connection?
Posted by CountyThen6572@reddit | learnprogramming | View on Reddit | 3 comments
Hi,
I built a small Python script to monitor internet connection and detect outages.
It currently:
- checks connectivity (ping/requests)
- logs downtime
- has basic retry logic
I’m trying to improve it and make it more robust.
What would you suggest?
For example:
- better way to handle connection checks?
- logging improvements?
- structure or performance ideas?
I’d really appreciate any advice from more experienced devs.
Faith1_2@reddit
Impressive! practical project. I’d suggest structured logging, simple alerts, and maybe async checks, happy to help if you want to expand it!
BrannyBee@reddit
Improve the tool? Or improve your Python ability? Cause those are potentially very different things.
For both though, you could look into Cython or the Python C API. You dont use that stuff to recode your entire app, but you find the bits that are tougher for Python to do fast, and only for those parts you use either of those methods to "drop down" to C.
One of Pythons strongest benefits is how easy it is to write simple code, and when you need to it can easily hook up some low level language power. Python is "slow", but its also used for a ton of things that require being fast. Good for learning and addressing performance, but also you dont lose any extensibility or what you already have the same way you would if you just rewrote everything in C.
For example, you could look into how to profile your script, which will be some python library that'll be nice to have added anyway if you're just wanting to add stuff. Then you can use that to find which parts you may be stressing Python a bit more than others as candidates to optimize.
I doubt you're running into this with your simple script, but imagine if you shove it into a really old outdated Raspberry Pi and it causes a bottleneck on the CPU, that's the exact kinda scenario that tells you its time to drop down to a lower level in order to fix. You can get really good at Python, but if you wanna master Python you gotta be able to do that or at least understand it when its happening, you've 100% already used libraries that do this for you in Python.
EveryProcedure7617@reddit
Maybe add multiple endpoints for checking instead of just one? Like ping google, cloudflare, and maybe github so if one is down you don't get false positive
Also you could track response times not just up/down status - helps identify when connection is getting slow before it completely fails. I learned this when my internet was being weird for weeks but only logging the complete outages