How to detect if my WiFi is enabled or not with Python?

Viewed 37

About question

  • Hello everyone. As I stated as in question title I am wondering "How to detect if my WiFi is enabled or not with Python?".

What have I tried?

  • I've tried code, that you can find in code sample below, but that code doesn't fit my needs.

    import urllib.request
      def connect(host='http://google.com'):
          try:
              urllib.request.urlopen(host) 
              return True
          except:
              return False
      # test
      if connect():
          print("Connected")
      else:
          print("No internet.")
    

Why does it not fit my needs?

  • It doesn't fit my needs because it will say "No internet" regardless if I am actually connected to network and have WiFi enabled or not. What I need is a way to firstly check if my internet is switched on or not and then if I have internet access or not.

Any ideas/solutions will come in handy and I think that the reason why I am not able to solve this is my lack of experience when it comes to Python.

0 Answers
Related