Best way to check if there is internet?

Akkana Peck akkana at shallowsky.com
Wed Feb 23 11:28:40 EST 2022


2QdxY4RzWzUUiLuE at potatochowder.com writes:
> I think someone said it way upthread:  don't check, just do whatever you
> came to do, and it will work or it will fail (presumably, your program
> can tell the difference, regardless of a past snapshot of being able to
> retrieve data from an arbitrary URL).
> 
> EAFP, anyone?

Yes, but the code being discussed is still helpful, if only for
error handling: yes, the network isn't fully up, but *why" isn't it?

while True:
    try:
        do_whatever_I_came_to_do()
    except NetworkError:
        net_config_with_good_error_detection()

Aside from error handling, it's useful in a network-up script:
when you've just enabled a network, it's good to check right then if
there's a captive portal and deal with it. If you just assume that
the first thing the user wants is to go to an http: page in a
browser, and so don't bother to check for a captive portal,
that'll be annoying for people who want to fetch IMAP mail,
or run ssh, or load an https: page.

        ...Akkana


More information about the Python-list mailing list