如何在Python中使用NordVPN?

Jaw*_*ans 3 python selenium urllib beautifulsoup python-requests

我想知道如何使用 NordVPN 服务器在使用 Python 进行 Web 自动化期间更改 IP 地址。假设以这种方式将其与 mechanize 一起使用:

br.open("url", proxies="nordvpn proxy")
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Sik*_*eph 5

我知道这可能有点晚了,但最近我遇到了与您的问题类似的挑战。

一般来说,我使用 python 来执行 Nord 的 Linux 命令。

import subprocess
import time
import subprocess, re, random, datetime

    # Nordvpn shinanigance skip to line 105
def getCountries():
    """
    This function will return a list of the current countries with available servers for your nordvpn account.
    """
    nord_output = subprocess.Popen(["nordvpn", "countries"], stdout=subprocess.PIPE)
    countries = re.split("[\t \n]", nord_output.communicate()[0].decode("utf-8"))
    while "" in countries:
        countries.remove("")
    return countries

def chooseRandom(country_list):
    """
    This function will randomly choose a country out of the available countries list.
    """
    return country_list[random.randrange(0, len(country_list))]

def logIn(random_country):
    """
    This function will take the randomly chosen country and attempt to log in to NordVPN using that country.
    """
    print("{} has been selected as the random country.".format(random_country))
    # subprocess.call(["nordvpn", "c", random_country])
    cmd = ["nordvpn", "c", random_country]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    o, e = proc.communicate()
    if "Whoops! We couldn't connect you" in o.decode('ascii'):
        print("not connected. Retrying...")
        logIn(chooseRandom(getCountries()))
    else:
        print("Connected to {}".format(random_country))

def checkConnection():
    print("checkConnection connection!")
    cmd = ['nordvpn', 'c']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    o, e = proc.communicate()
    if "Connected" in o.decode('ascii'):
        return True
    else:
        return False

def tryConnection():
    print("Trying connection!")
    cmd = ['nordvpn', 'c']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    time.sleep(2)
    if checkConnection() == True:
        print("Sucessfull Connection with:\n" + email +" " +password)
    else:
        print("Failed")
        cmd = ['nordvpn', 'logout']
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        print("Logged out")

def loginWithPassword(email, password):
    cmd = ['nordvpn', 'login', '--username', email, '--password', password]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # print(proc)
    o, e = proc.communicate()
    stdout = proc.communicate()[0]
    # print (format(stdout))
    if "is not correct" in o.decode('ascii'):
        print("Wrong password")

    if "already logged in" in o.decode('ascii'):
        print("Already logged in")
        nord_output = subprocess.Popen(["nordvpn", "status"], stdout=subprocess.PIPE)
        status = re.split("[\r \n :]", nord_output.communicate()[0].decode("utf-8"))[-2]
        if status == "Disconnected":
            print("Disconnected from nord!")
            logIn(chooseRandom(getCountries()))
        else:
            print("Connected...")
            print("Calling diconnecting to randomize...")
            subprocess.call(["nordvpn", "disconnect"])
            logIn(chooseRandom(getCountries()))
Run Code Online (Sandbox Code Playgroud)

所以,你只需要打电话

loginWithPassword("email", "password")
Run Code Online (Sandbox Code Playgroud)

非常感谢我向这个人借了一些东西

NordVPN_随机化器

  • 您必须先安装nordvpn。请点击此链接了解如何安装nordvpn https://support.nordvpn.com/Connectivity/Linux/1325531132/Installing-and-using-NordVPN-on-Debian-Ubuntu-Raspberry-Pi-Elementary-OS-and-Linux-薄荷.htm (2认同)