2

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.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
helloworld
  • 166
  • 1
  • 2
  • 9
  • 1
    Does [this](https://stackoverflow.com/a/62115290/16136190) help? – The Amateur Coder Jan 21 '22 at 19:16
  • 1
    Hey @TheAmateurCoder , thanks for the reply! I couldn't really understand the code that you've linked me to, so therefore I don't know if that particular code fits my needs or not. Anyways, I was able to find what I am looking for by reading answers section from that question, so I am really thankful. – helloworld Jan 21 '22 at 19:27

0 Answers0