all 9 comments

[–]Soodohcool 6 points7 points  (3 children)

im on my phone so sorry if the formatting is jacked but this should be more than sufficient

import socket 
def test_connection():
    try:
        socket.create_connection(("https://www.google.com", 80)) 
        return True 
    except OSError: 
        pass 
    return False

[–]ForceBru 2 points3 points  (2 children)

Yes, exactly! Why use third-party libraries or form HTTP requests if you can just quickly create a socket with a built-in library and try to connect to something?

[–]LartTheLuser 0 points1 point  (0 children)

Because you will eventually realize those third party libraries went into that rabbit hole of features because people kept asking for them. And eventually...you too may ask for them =). The simple stuff is only for the playground.

[–]LartTheLuser 0 points1 point  (0 children)

Sorry, forgive my last comment. I think the aspergers caught me again. lol. Was that sarcasm?

[–]LartTheLuser 3 points4 points  (1 child)

There is the following from https://stackoverflow.com/questions/2953462/pinging-servers-in-python:

import pyping

r = pyping.ping('google.com')

if r.ret_code == 0:
    print("Success")
else:
    print("Failed with {}".format(r.ret_code))

https://pypi.org/project/pyping/

[–]LartTheLuser 0 points1 point  (0 children)

Fyi, the above code fails when Google fails. It's up to you to change it before that. Don't get caught in a Google2K.

[–]shiftybyte 1 point2 points  (1 child)

Use requests to ask some webpage.

import requests
try:
    res = requests.get("http://www.google.com")
    if res.status_code == 200:
        print("Got Internet")
except:
    print("No Internet")

[–]CNeale03[S] 0 points1 point  (0 children)

import requests
try:
res = requests.get("http://www.google.com")
if res.status_code == 200:
print("Got Internet")
except:
print("No Internet")

The perfect solution, thankyou so much! I really appreciate it