Nordvpn servers instead of python requests proxies | Works for Selenium, Urllib, Mechanize …

Scraping data is amazing. Imagine that a simple script can save you lots of boring and time-consuming tasks and during this process, we might need to use proxies to hide our IP addresses.

Selmi Abderrahim
4 min readDec 24, 2021

Today, I’ll show you a safe way to implement the NordVpn servers to do the same job as proxy servers can do.

How To Connect to NordVPN With Terminal or Command Prompt?

On Windows

First, you need to make sure to install Nordvpn first, in Windows, it’s pretty simple Download the installer, launch it and that’s it.

After installing Nordvpn on your machine, you’ll be able to use it from the Nordvpn GUI app, but to use it from the CMD, you must first navigate to the directory of NordVPN installation which we will use to access the nordvpn.exe file.
In my case, this is the path: “C:\Program Files\NordVPN\”

So, after we open the CMD, we execute this command:

cd "C:\Program Files\NordVPN\"

After that, enter any of these commands:

To connext to a random server, you can type:

nordvpn connect -c

Or to connect to a specefic server name.

nordvpn connect -c -n "France #352"

To connect to specefiv country:

nordvpn connect -c -g "United States"

And finally, to disconnect from the server, type:

nordvpn -d

On Linux

Also, in Linux, you have to make sure you have Nordvpn installed, in case it’s not, follow these steps:

Download the NordVPN repo setup .deb package from here.

Move to the Downloads folder and Open the terminal and run the following:

sudo apt-get install nordvpn-release_1.0.0_all.deb

Update the apt-get package list

sudo apt-get update

Install NordVPN

sudo apt-get install nordvpn

After installing nordvpn on your Linux machine, you can login to your account from the Terminal using this command:

nordvpn login

And to connect to random server, type:

nordvpn connect

But to connect to a specific server or country, type:

nordvpn connect country

How To Use NordVpn Servers In Python

We will create a python function that allows the user to change the IP address from one of the +5000 Nordvpn servers. This function should work on Windows, Linux, and Mac.

To be able to connect to specefic country, we need to get a list of these countries:

linux_countries = ['al', 'ar', 'au', 'at', 'br', 'bg', 'ca', 'cl', 'cr', 'hr', 'cy', 'cz', 'dk', 'ee', 'fi', 
'fr', 'ge', 'de', 'gr', 'hk', 'hu', 'is', 'in', 'id', 'ie', 'il', 'it', 'jp', 'lv', 'lu', 'my',
'mx', 'md', 'nl', 'nz', 'mk', 'no', 'pl', 'pt', 'ro', 'rs', 'sg', '', 'si', 'za', 'kr', 'es',
'se', 'ch', 'tw', 'th', 'tr', 'ua', 'So', 'uk', 'us']
windows_countries = ['United States', 'Canada', 'Argentina', 'Brazil', 'Mexico', 'Costa Rica', 'Chile',
'United Kingdom', 'Germany', 'France', 'Netherlands', 'Sweden', 'Switzerland',
'Denmark', 'Poland', 'Italy', 'Spain', 'Norway', 'Belgium', 'Ireland', 'Czech Republic',
'Austria', 'Portugal', 'Finland', 'Ukraine', 'Romania', 'Serbia', 'Hungary', 'Luxembourg',
'Slovakia', 'Bulgaria', 'Latvia', 'Greece', 'Iceland', 'Estonia', 'Albania', 'Croatia',
'Cyprus', 'Slovenia', 'Moldova', 'Bosnia and Herzegovina', 'Georgia', 'North Macedonia',
'Turkey', 'South Africa', 'India', 'Israel', 'Turkey', 'United Arab Emirates', 'Australia',
'Taiwan', 'Singapore', 'Japan', 'Hong Kong', 'New Zealand', 'Malaysia', 'Vietnam', 'Indonesia',
'South Korea', 'Thailand']

Yes, in Windows, we use the country name to connect to Nordvpn, but in Linux, we use only two letters of the country name.

Then, we need to import few modules:

import platform
import random
import os
import time

Now, here is our Nordvpn function:

def nordvpn():
version = platform.system()
if version == "Linux" or version == "Darwin":
ser = "nordvpn connect "+random.choice(linux_countries)+" > /dev/null 2>&1"
os.system(ser)
elif version == "Windows":
server = "nordvpn -c -g \'"+random.choice(windows_countries)+"\'"+" > /dev/null 2>&1"
os.system(server)
time.sleep(10)

First, we use the platform module to see which os we’re using, then, using the os module, we run the command line that allows us to connect to Nordvpn to a specific country.

Also, to make sure we connect to different servers each time, we use the random module to generate random country names.

Note: We used the > /dev/null to hide the output, since Nordvpn weill print a message to tell us that we are connected to the server X.

Testing

Now, let’s see if this will work with different web scraping libraries.

First, we need to know what is our IP address taht we have before using Nordvpn:

import requests
ip = requests.get('https://api.ipify.org').text
print(f"\nBefore Using NordVpn\nIp:\t{ip}")
  • Selenium Example
nordvpn()from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://api.ipify.org')
ip = driver.find_element_by_tag_name("body").text
print(f"\nWith Selenium\nIp:\t{ip}")
driver.close()
  • urllib3 Example
nordvpn()
import urllib3
http = urllib3.PoolManager()
resp = http.request("GET", "https://api.ipify.org")
ip = resp.data.decode("UTF-8")
print(f"\nWith Urllib3\nIp:\t{ip}")
  • Requests Example
nordvpn()ip = requests.get('https://api.ipify.org').text
print(f"\nWith Requests\nIp:\t{ip}")

And this is the result I got:

Before Using NordVpn
Ip: 45.133.192.68
With Selenium
Ip: 192.154.196.30
With Urllib3
Ip: 103.156.50.146
Before Using NordVpn
Ip: 179.48.249.141

The final code

import platform
import random
import os
import time
linux_countries = ['al', 'ar', 'au', 'at', 'br', 'bg', 'ca', 'cl', 'cr', 'hr', 'cy', 'cz', 'dk', 'ee', 'fi',
'fr', 'ge', 'de', 'gr', 'hk', 'hu', 'is', 'in', 'id', 'ie', 'il', 'it', 'jp', 'lv', 'lu', 'my',
'mx', 'md', 'nl', 'nz', 'mk', 'no', 'pl', 'pt', 'ro', 'rs', 'sg', '', 'si', 'za', 'kr', 'es',
'se', 'ch', 'tw', 'th', 'tr', 'ua', 'So', 'uk', 'us']
windows_countries = ['United States', 'Canada', 'Argentina', 'Brazil', 'Mexico', 'Costa Rica', 'Chile',
'United Kingdom', 'Germany', 'France', 'Netherlands', 'Sweden', 'Switzerland',
'Denmark', 'Poland', 'Italy', 'Spain', 'Norway', 'Belgium', 'Ireland', 'Czech Republic',
'Austria', 'Portugal', 'Finland', 'Ukraine', 'Romania', 'Serbia', 'Hungary', 'Luxembourg',
'Slovakia', 'Bulgaria', 'Latvia', 'Greece', 'Iceland', 'Estonia', 'Albania', 'Croatia',
'Cyprus', 'Slovenia', 'Moldova', 'Bosnia and Herzegovina', 'Georgia', 'North Macedonia',
'Turkey', 'South Africa', 'India', 'Israel', 'Turkey', 'United Arab Emirates', 'Australia',
'Taiwan', 'Singapore', 'Japan', 'Hong Kong', 'New Zealand', 'Malaysia', 'Vietnam', 'Indonesia',
'South Korea', 'Thailand']
def nordvpn():
version = platform.system()
if version == "Linux" or version == "Darwin":
ser = "nordvpn connect "+random.choice(linux_countries)+" > /dev/null 2>&1"
os.system(ser)
elif version == "Windows":
server = "nordvpn -c -g \'"+random.choice(windows_countries)+"\'"+" > /dev/null 2>&1"
os.system(server)
time.sleep(10)
#Before using NordVpn
import requests
ip = requests.get('https://api.ipify.org').text
print(f"\nBefore Using NordVpn\nIp:\t{ip}")
nordvpn()#Selenium Example
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path="./linux")
driver.get('https://api.ipify.org')
ip = driver.find_element_by_tag_name("body").text
print(f"\nWith Selenium\nIp:\t{ip}")
driver.close()
nordvpn()#urllib3 Example
import urllib3
http = urllib3.PoolManager()
resp = http.request("GET", "https://api.ipify.org")
ip = resp.data.decode("UTF-8")
print(f"\nWith Urllib3\nIp:\t{ip}")
nordvpn()#Requests Example
ip = requests.get('https://api.ipify.org').text
print(f"\nWith Requests\nIp:\t{ip}")

I hope that was helpful, if you faced any error, please comment bellow, I’ll reply to all of you.

--

--

Selmi Abderrahim

I’m a computer science student. I do freelancing in my free time. I am a big fan of affiliate marketing, it helped me a lot to monetize my daily stuff.