-3

I need Python to check if it's connected to the internet every time a loop runs. I need the output to be in a variable, a yes or no thing.

This is the code I tried but it never repeats in a loop.

try:
    socket.create_connection(("www.swisscows.com",80))
    b= [1]
    pass
except OSError:
    b= [0]
    pass      
khelwood
  • 55,782
  • 14
  • 81
  • 108
UNKNOWN
  • 105
  • 10

1 Answers1

3

I would suggest to execute a ping request against a web site that you know should be online at all time. See the response here: Pinging servers in Python

Lydia van Dyke
  • 2,466
  • 3
  • 13
  • 25
  • the ping works but the program won't repeat – UNKNOWN Apr 04 '20 at 22:24
  • 1
    You updated your code with the ping method as given in https://stackoverflow.com/questions/2953462/pinging-servers-in-python and it still does not work? In that case the problem seems to be somewhere else in your code. Can you post the complete loop that is not working? – Lydia van Dyke Apr 04 '20 at 22:27