【问题标题】:Python Output ErrorPython 输出错误
【发布时间】:2017-04-02 15:32:53
【问题描述】:

我正在使用 python 制作一个简单的脚本来检查 IP 是否在 vpn 上,但我正在使用 vpn 进行测试,并且我使用了我的真实 IP,结果都显示为“vpn not使用中”,即使它是,输出(“来自 curl 命令)也是如此。

#!/usr/bin/env python
import os
import subprocess
from subprocess import Popen,PIPE
import socket
import time
import optparse
parser = optparse.OptionParser()
parser.add_option("-t", "--hst", action="store", dest="host", help="Target host to check if is using VPN", default=False)
options, args = parser.parse_args()
host = socket.gethostbyname(options.host)
def hostname_check(hst):
        check = subprocess.Popen(['curl ipinfo.io/'+hst], shell=True)
        if('No Hostname' in str(check)):
                print("Host: %s - Virtual Private Network in use") % host

        elif('No Hostname' not in str(check)):
                print("Host: %s - Virtual Private Network NOT in use") % host
hostname_check(options.host)

要执行的命令:./vpnchecker.py --hst 162.224.81.174 输出:

    Host: 162.244.81.174 - Virtual Private Network NOT in use
user@LAPTOP:~$ {
  "ip": "162.244.81.174",
  "hostname": "No Hostname",
  "city": "",
  "region": "",
  "country": "RO",
  "loc": "46.0000,25.0000",
  "org": "AS19624 Data Room, Inc"
}

它应该是:Host: 162.244.81.174 - Virtual Private Network in use,我不希望 curl 命令的输出显示在屏幕上。

【问题讨论】:

  • 是我还是你错过了parser.add_option及以后的一堆标点符号(引号、括号、逗号)。
  • @abccd 不,解析器很好,我在很多程序中都写过。
  • 我的意思是您在此处发布的代码,尝试运行它。可能你复制粘贴错了
  • 是的,肯定会关闭 parser.add_option 行中缺少的 ",并且可能在上面的原始代码中还有一些其他内容。
  • @abccd,是的,对不起,当我复制和粘贴时,我的 parser.add_option 的关闭被切断了,不幸的是这不是错误,我现在修复它,然后尝试在你的电脑告诉我你是否遇到同样的问题,我整天都在努力解决它......开始让我对我的电脑做恶意的事情哈。

标签: python curl popen


【解决方案1】:

我使用了一种不同的方法来使用请求,因此我没有使用 curl 来提取 Web 数据,而是从 requests 库中获得了相同的数据。这是任何有兴趣的人的代码:

#!/usr/bin/env python
import os
import subprocess
from subprocess import Popen,PIPE
import socket
import time
import requests
import optparse
parser = optparse.OptionParser()
parser.add_option("-t", "--hst", action="store", dest="host", help="Target host to check if is using VPN", default=False)
from requests import *
options, args = parser.parse_args()
host = socket.gethostbyname(options.host)
def hostname_check(hst):
        check = requests.get('http://ipinfo.io/'+hst).text
        if('No Hostname' in check):
                print("Host: %s - Virtual Private Network in use") % host

        elif('No Hostname' not in check):
                print("Host: %s - Virtual Private Network NOT in use") % host
hostname_check(options.host)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2021-11-27
    • 1970-01-01
    相关资源
    最近更新 更多