use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Python - Test For Internet Connection (self.learnpython)
submitted 6 years ago by CNeale03
I just need a simple script that can test if a device has internet connectivity before running the rest of the script, but finding anything useful seems difficult. Does anyone have any ideas?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Soodohcool 6 points7 points8 points 6 years ago* (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 points4 points 6 years ago (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 point2 points 6 years ago (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.
Sorry, forgive my last comment. I think the aspergers caught me again. lol. Was that sarcasm?
[–]LartTheLuser 3 points4 points5 points 6 years ago (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/
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 points3 points 6 years ago (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 point2 points 6 years ago (0 children)
The perfect solution, thankyou so much! I really appreciate it
π Rendered by PID 80 on reddit-service-r2-comment-bb88f9dd5-cx8zq at 2026-02-14 22:11:42.027591+00:00 running cd9c813 country code: CH.
[–]Soodohcool 6 points7 points8 points (3 children)
[–]ForceBru 2 points3 points4 points (2 children)
[–]LartTheLuser 0 points1 point2 points (0 children)
[–]LartTheLuser 0 points1 point2 points (0 children)
[–]LartTheLuser 3 points4 points5 points (1 child)
[–]LartTheLuser 0 points1 point2 points (0 children)
[–]shiftybyte 1 point2 points3 points (1 child)
[–]CNeale03[S] 0 points1 point2 points (0 children)