How to Run Windscribe VPN in Windows with Python

Deepanshu Bhalla Add Comment

In this tutorial, we will show you how to run Windscribe VPN in Windows using Python Code. Windscribe is a popular VPN service that offers several features. Windscribe's free version maintains the same speed as the paid plans.

Python code to run Windscribe VPN in Windows

The following code is a Python script that interacts with the Windscribe VPN client using its command-line interface (CLI) to perform connection and disconnection actions.

import os

def windscribe(action, location=None):
    windscribe_cli_path = r"C:\\Program Files\\Windscribe\\windscribe-cli.exe"
    
    if location is None:
        command = f'"{windscribe_cli_path}" {action}'
    else:
        command = f'"{windscribe_cli_path}" {action} {location}'
    
    os.system(command)

# Connect
windscribe("connect", "US Central")

# Disconnect
windscribe("disconnect")

Make sure the os module is installed. The os module is a standard Python library that provides a way to interact with the operating system, including running system commands. The windscribe_cli_path variable is set to the file path of the Windscribe CLI executable. Change this file path if the file location of CLI on your system is different.

We have set the location "US Central". This will result in the Windscribe VPN client connecting to a server located in the US Central region. You can choose any location you want based on the location Windscribe supports.

How to Randomly Connect to VPN Locations

The following code adds more functionality to the previous script. It randomizes the selection of VPN server locations and also logs the actions and outcomes into a log file.

# Randomly select location
import os
import random
from datetime import datetime

def windscribe_random(action, connect_list=None):
    
    # Connect
    windscribe_cli_path = r"C:\\Program Files\\Windscribe\\windscribe-cli.exe"    
    if connect_list is not None:
        location = random.choice(connect_list)        
        output = os.popen(f'"{windscribe_cli_path}" {action} {location}').read()
        print(output)
    
        # Write Log
        log = os.path.join(os.getcwd(), "windscribe-log.txt")
        log_datetime = f"{datetime.now():%Y-%m-%d %H:%M:%S} {output}"
        log_datetime2 = "\n".join(["", log_datetime])
        if os.path.isfile(log):
            with open(log, "a") as f:
                f.write(log_datetime2)
        else:
            with open(log, "w") as f:
                f.write(log_datetime)
    else:
        os.popen(f'"{windscribe_cli_path}" {action}')
            

# Connect
connect_list = ["US Central", "US East", "US West", "Canada East", "Canada West"]
windscribe_random("connect", connect_list)

# Disconnect
windscribe_random("disconnect")

The above code randomly selects a location from the list ["US Central", "US East", "US West", "Canada East", "Canada West"]

The script creates records in a log file that include the current date and time from datetime.now() and appends the command output. It then checks if a log file named "windscribe-log.txt" exists in the current working directory. If the file exists, the log entry is appended to it. If not, a new log file is created.

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Run Windscribe VPN in Windows with Python"
Next → ← Prev