Best way to check if there is internet?

Avi Gross avigross at verizon.net
Tue Feb 22 13:37:15 EST 2022


As usual, his discussion is wandering a bit.

Yes, some things are not reliable but sometimes a heuristic answer is better than none.

Internet connections are beyond flaky and worse the connection you want may be blocked for many reasons even if other connections you try are working. Any number of routers can lose power or you personally may be blocked by something if they deem the place you want unsafe and much more.

In Statistics, you often work not with TRUE and FALSE but with ranges of probability and you may deem something as tentatively true if it is 95% or 99% likely it did not happen by chance given your sample.

But for a long-running program, checking if the internet is up ONCE is clearly not good enough if it runs for months at a time. Checking right before making a connection may be a bit much too.

But computers often spend a majority of their time doing things like checking. If I am using a GUI and the process is waiting to see if I type some key (or even am depressing the Shift key which normally does not generate any symbols by itself) and also testing if my mouse is moving or has moved over an area or one of several buttons has been clicked, then what may be happening is something in a loop that keeps checking and when an event has happened, notifies something to continue and perhaps provides the results. Something along those lines can happen when say a region of memory/data is being shared and one part wants to know if the memory is still locked or has finally been written to by another part and can be read and so on.

For something important, it is not enough to check if a file system is mounted and available and then write the file and move on. You may want to not only test the exit status of the write, or arrange to catch exceptions, but to then re-open the file and read in what you wrote and verify that it seems correct. In some cases, you get even more paranoid and save some kind of log entry that can be used to recreate the scenario if something bad happens and store that in another location in case there is a fire or something. 

So the answer is that it is up to you to decide how much effort is warranted. Can you just try a connection and react if it fails or must you at least get a decent guess that there is an internet connection of some kind up and running or must it be ever more bulletproof?

A scenario might be one where you are providing info and can adjust depending on various settings. If the user has an internet connection on, you can use video and graphics and so on that result in something current. But if they have settings suggesting they want to minimize the data they send and receive, the same request may result in the browser you invoked showing them just some alternative text. You have no way of knowing. Again, if the internet is not up, you may just present your own canned text, perhaps dated. Or, you can suggest to the user they turn on the internet and try again, or ...

It sounds to me like your main issue is not whether the internet is up right now but whether the user has an internet connection. If they run the program on a laptop at home or work then they may well be connected, but sitting in their car in some parking lot, may well not be.




-----Original Message-----
From: Abdur-Rahmaan Janhangeer <arj.python at gmail.com>
To: Python <python-list at python.org>
Sent: Tue, Feb 22, 2022 12:32 pm
Subject: Re: Best way to check if there is internet?


Well, nice perspective.

It's a valid consideration, sound theory
but poor practicality according to me.

It you view it like this then between the moment
we press run or enter key on the terminal,
our Python interpreter might get deleted.

We never know, it can happen.

Though it's nice to go in and catch exceptions,
if you have a long operation upcoming it might be nice
to see if your key element is present.

Much like checking if goal posts are present before
starting a football game. You can of course start
the match, pass the ball around and when shooting
you stop the match as you realise that the goal posts
are not around.

Checking by exceptions is what the snippet
i shared does. But we check for the ability to do it
before we start.

Of course, it can change in the seconds that follow. But it's too much pure
logic at work.

-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list