-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
  • 52,115
  • 13
  • 74
  • 94
UNKNOWN
  • 105
  • 9

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,308
  • 3
  • 11
  • 24
  • 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