Tutorial Example

Best Practice to Python Enable and Disable Wifi Connection on Win 10 – Python Tutorial

There is a problem on my wifi connection, if i have not used my computer for a long time, the wifi connection will be cut automatically, which causes our python downloader application to be stop. I have to disable and enable my wifi connection to access internet manually.

Is there any way to disable and enable wifi connection on win 10 automatically? The answer is yes. In this tutorial, we will use python to disable and enable wifi connection.

Step 1. Check your wifi connection name

You can open your internet and check the name of your wifi connection, as to me, the name wifi connection is WLAN, we will use this wifi name to enable and disable wifi connection.

Step 2. Use Python to enable and disable wifi connection

Import python library

import os
import time

In this example, we will use python os library to call netsh command on win 10 to enable and disable wifi connection.

Build enable and disable wifi connection command

wifi_name = 'WLAN'

enable_wifi = 'netsh interface set interface "'+wifi_name+'" enabled'
disable_wifi = 'netsh interface set interface "'+wifi_name+'" disabled'

Disable wifi connection

#disable wifi, if network is OFFLINE
os.popen(disable_wifi)

Run this code, you will find:

Enable wifi connection

time.sleep(10)
#enable wifi
os.popen(enable_wifi)