首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中使用NordVPN?

如何在Python中使用NordVPN?
EN

Stack Overflow用户
提问于 2021-08-08 18:14:27
回答 1查看 696关注 0票数 2

我想知道如何使用NordVPN服务器在使用Python自动化的过程中更改IP地址。让我们以这样的方式将其与mechanize一起使用:

代码语言:javascript
复制
br.open("url", proxies="nordvpn proxy")

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-11-12 19:06:59

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

通常,我使用python来执行Nord的Linux命令。

代码语言:javascript
复制
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()))

所以,你只要打个电话

代码语言:javascript
复制
loginWithPassword("email", "password")

非常感谢这个家伙,我向他借了一些东西

NordVPN_Randomizer

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68703551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档