Python Forum
Enable & Disable Wireless connection by using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Enable & Disable Wireless connection by using python (/thread-15358.html)



Enable & Disable Wireless connection by using python - aniyanetworks - Jan-14-2019

Sorry if you find this questions very easy and basic for you. I am New in Python and trying to learn every day.

My Questions: How can I disable and enable Wi-Fi/ Wireless network by using Python?

My Goal is to, Create and GUI with 2 buttons, i will enable the Wifi and another one will disable the Wifi, where the user can click to disable and enable the Wi-Fi network.

Please help and advise. Thank you


RE: Enable & Disable Wireless connection by using python - Gribouillis - Jan-14-2019

If you know how to do it from a terminal, you can readily call the command from a python script with the subprocess module.


RE: Enable & Disable Wireless connection by using python - aniyanetworks - Jan-14-2019

(Jan-14-2019, 04:11 PM)Gribouillis Wrote: If you know how to do it from a terminal, you can readily call the command from a python script with the subprocess module.

Hello Griboullis, Thanks for your reply.
I was using Tkinter GUI to create 2 buttons and i don't know which library or function should I use.
Hope this will help.
Thanks
MP


RE: Enable & Disable Wireless connection by using python - Gribouillis - Jan-14-2019

Do you know how to enable or disable the wireless connection from a terminal in your OS (without python at all)?


RE: Enable & Disable Wireless connection by using python - aniyanetworks - Jan-14-2019

(Jan-14-2019, 04:17 PM)Gribouillis Wrote: Do you know how to enable or disable the wireless connection from a terminal in your OS (without python at all)?

Yes, i do have the script.
Quote:netsh interface set interface "Wi-Fi" DISABLED
Quote:netsh interface set interface "TPALANDOC" ENABLED

and it works perfectly. but i want to make it more user-friendly.
Please advise.
Thank you
MP


RE: Enable & Disable Wireless connection by using python - Gribouillis - Jan-14-2019

Then you can use (for python >= 3.5)
import subprocess
result = subprocess.run(["netsh", "interface", "set", "interface", "Wi-Fi", "DISABLED"])
print("FAILED..." if result.returncode else "SUCCESS!")



RE: Enable & Disable Wireless connection by using python - aniyanetworks - Jan-14-2019

(Jan-14-2019, 04:25 PM)Gribouillis Wrote: Then you can use (for python >= 3.5)
import subprocess
result = subprocess.run(["netsh", "interface", "set", "interface", "Wi-Fi", "DISABLED"])
print("FAILED..." if result.returncode else "SUCCESS!")

Thank you ,
I am getting the following error, even though i am running cmd as Administrator
Quote:The requested operation requires elevation (Run as administrator).

FAILED...
Please advise.
Thank you
MP.


RE: Enable & Disable Wireless connection by using python - Gribouillis - Jan-14-2019

I dont know windows well enough, but you can try to add 'runas', '/noprofile', '/user:Administrator', before 'netch'. Another solution would perhaps be to run your tkinter script as administrator.


RE: Enable & Disable Wireless connection by using python - aniyanetworks - Jan-14-2019

(Jan-14-2019, 04:57 PM)Gribouillis Wrote: I dont know windows well enough, but you can try to add 'runas', '/noprofile', '/user:Administrator', before 'netch'. Another solution would perhaps be to run your tkinter script as administrator.

Hi Gribouillis,
Thanks for your reply and solutions, But it did work for me.
Sorry about it.
Thanks for your help.
Manam


RE: Enable & Disable Wireless connection by using python - aniyanetworks - Jan-15-2019

Hello,
I tried to use this code and modify, looks like it doesn't do anything.
any suggestions, Please
import os
os.system("netsh interface show interface")

def enable():
os.system("netsh interface set interface 'Wi-Fi' enabled")

def disable():
os.system("netsh interface set interface 'Wi-Fi' disabled")


My Output:
Quote:My OutPut:
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Connected Dedicated Wi-Fi
Disabled Disconnected Dedicated TPALAN


Process finished with exit code 0

Thanks
MP