Python returns False even if internet is connected

前端 未结 2 2078
天涯浪人
天涯浪人 2021-01-23 09:22

I am trying to learn python. This a script I wrote to check internet connection

import os
import urllib2
from time import sleep
REMOTE_SERVER = \"www.google.co.u         


        
相关标签:
2条回答
  • 2021-01-23 10:06

    Since you haven't imported socket, your reference to socket.gethostbyname will fail with a NameError. But you're catching and silencing every single exception in your try/except block, including that very error. You should never ever do a blank except in the first place, and especially never just to have pass.

    Remove that try/except, or limit it to exceptions you know you can handle.

    0 讨论(0)
  • 2021-01-23 10:27

    Basically, never do this:

    try:
        something()
    except:
        pass
    

    Which is the Python equivalent of the good'ol Visual Basic anti-pattern, if you recall:

    On Error Resume Next
    

    Leading to unmaintainable and impossible to debug code. Simply because when a problem arises, you don't know what happened (and you don't even know that there is any problem at all).

    In your particular case, I suggest you remove the try/except block so that you can know which exception is raised in the traceback. Then, you'll be able to fix it.

    0 讨论(0)
提交回复
热议问题