从Python程序中获取nordvpn状态

问题描述 投票:0回答:1

我正在使用

nordvpn_connect
软件包连接到 VPN 服务器。

例如:

from nordvpn_connect import initialize_vpn, rotate_VPN, close_vpn_connection

settings = initialize_vpn("uk2388")  # starts nordvpn and stuff
rotate_VPN(settings)  # actually connect to server

# ///// Stuff

close_vpn_connection(settings)

我希望能够检查连接的状态。在终端上,可以使用

nordvpn status
轻松完成此操作,但 文档 没有提及类似的内容。

所以目前我只是使用

subprocess
:

def check_vpn_status():
    """Check the VPN connection status by parsing the output of `nordvpn status` command."""
    try:
        output = subprocess.check_output(["nordvpn", "status"]).decode('utf-8')
        if "Status: Connected" in output:
            return True
    except subprocess.CalledProcessError as e:
        print(f"Error checking VPN status: {e}")
    return False

有更好的方法吗?

python subprocess vpn
1个回答
1
投票

nordvpn_connect
不提供任何功能来检查与其服务器的连接性,您使用
subprocess
所做的是一种实用的方法。

© www.soinside.com 2019 - 2024. All rights reserved.