Connecting to a New Wi-Fi Network

Now, connecting to a new Wi-Fi involves a couple of more steps. To connect to a new network, we must first add this new Wi-Fi Network profile to our system using an .XML file. This makes that Wi-Fi network, a known SSID, and we can now successfully connect to it using the above steps.

Approach:

  • Step 1: Import the os library
  • Step 2: Set up the new Wi-Fi Network’s XML configuration
  • Step 3: Select the Wi-Fi Network
  • Step 4: Add this profile to your system
  • Step 5: Connect to the Wi-Fi network

 Implementation:

Python3




# import module
import os
 
# function to establish a new connection
def createNewConnection(name, SSID, password):
    config = """<?xml version=\"1.0\"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>"""+name+"""</name>
    <SSIDConfig>
        <SSID>
            <name>"""+SSID+"""</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>"""+password+"""</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>"""
    command = "netsh wlan add profile filename=\""+name+".xml\""+" interface=Wi-Fi"
    with open(name+".xml", 'w') as file:
        file.write(config)
    os.system(command)
 
# function to connect to a network   
def connect(name, SSID):
    command = "netsh wlan connect name=\""+name+"\" ssid=\""+SSID+"\" interface=Wi-Fi"
    os.system(command)
 
# function to display avavilabe Wifi networks   
def displayAvailableNetworks():
    command = "netsh wlan show networks interface=Wi-Fi"
    os.system(command)
 
 
# display available netwroks
displayAvailableNetworks()
 
# input wifi name and password
name = input("Name of Wi-Fi: ")
password = input("Password: ")
 
# establish new connection
createNewConnection(name, name, password)
 
# connect to the wifi network
connect(name, name)
print("If you aren't connected to this network, try connecting with the correct password!")


Output:

Explanation:

First, we define the createNewConnection function which takes the parameters name, SSID, and password, which are all strings that we used to complete to config variable. The config variable is a string that helps us define the XML configuration for a new Wi-Fi Network.

Then, we take the input from the user for the SSID name and Password. They are then fed into XML code which is then added as a profile using the following lines of code:

command = "netsh wlan add profile filename=\""+name+".xml\""+" interface=Wi-Fi"
    with open(name+".xml", 'w') as file:
        file.write(config)
    os.system(command)

We can now connect to the Wi-Fi using the same commands we used earlier in this article and connect to the network as if it was a known one. 



How to connect WiFi using Python?

Seeing a computer without an active internet connection today is next to impossible. The Internet has been of the utmost importance in the 21st Century. There are multiple ways one can connect their machine to the Internet. The first being, the traditional cables, i.e. the Ethernet, and the other being, the modern Wireless Fidelity Systems or Wi-Fi as we all know it. Wi-Fi has made life easier and faster for all of us. With a touch of the thumb or a click of the mouse, we get connected to a limitless ocean of information and resources almost instantaneously. In this article, we will accomplish the same task with a High-Level modern programming language, like Python

Similar Reads

Connecting to a Known WiFi Network

Here we are going to connect to a previously connected WiFi network....

Connecting to a New Wi-Fi Network

...

Contact Us