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

By | September 30, 2019

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

Win 10 WLAN 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

Run this code, you will find:

python disable wlan

Enable wifi connection

#enable wifi

Leave a Reply

Your email address will not be published. Required fields are marked *