【问题标题】:How would I fix this script with the wrong output?我将如何使用错误的输出修复此脚本?
【发布时间】:2023-03-12 09:37:02
【问题描述】:

它以多种方式搞砸了

代码如下:

import threading
import sys
import time
import os
import subprocess
import requests
import threading

ip = sys.argv[1:]

website_loaded = False

def verify():
    while website_loaded == False:
        sys.stdout.write("Verifying IP    \r")
        time.sleep(0.5)
        sys.stdout.write("Verifying IP.   \r")
        time.sleep(0.5)
        sys.stdout.write("Verifying IP..  \r")
        time.sleep(0.5)
        sys.stdout.write("Verifying IP... \r")
        time.sleep(0.5)
        continue

verify_thread = threading.Thread(target = verify, args = ())
verify_thread.start()

ping = os.system("ping %s >nul" % str(ip))
ipVerification = subprocess.getoutput(str(ping))

website_loaded = True
if ipVerification == "Ping request could not find host %s. Please check the name and try again." % ip:
    print("Invalid IP. Please try again")
    print("Press any key to exit")
    os.system("pause >nul")
    exit()
elif ipVerification.find('PING: transmit failed. General failure.') != -1:
    print("Invalid IP. Please try again")
    print("Press any key to exit")
    os.system("pause >nul")
    exit()
website_loaded = False

def loading():
    while website_loaded == False:
        sys.stdout.write("Loading    \r")
        time.sleep(0.5)
        sys.stdout.write("Loading.   \r")
        time.sleep(0.5)
        sys.stdout.write("Loading..  \r")
        time.sleep(0.5)
        sys.stdout.write("Loading... \r")
        time.sleep(0.5)
        continue

loading_thread = threading.Thread(target = loading, args = ())
loading_thread.start()

info = requests.get("http://ip-api.com/json/%s" % ip).json()

website_loaded = True

print(info)

当我在cmd中输入“ipinfo.py 55”时,输出是“{'message': 'invalid query', 'query': '[]', 'status': 'fail'} 正在加载... P...”,我不知道为什么。我希望它说 IP 无效,但由于某种原因,它正在做这个奇怪的事情

【问题讨论】:

  • 变量ip 是一个列表,所以你实际上是在发出这个请求:ip-api.com/json/['55']
  • @rdas 我怎样才能让它不是一个列表?
  • ip = sys.argv[1]
  • @rdas 给出错误IndexError: list index out of range
  • 您没有使用至少一个参数调用脚本。

标签: python python-3.x python-requests subprocess sys


【解决方案1】:

要获取单个 IP,请执行以下操作:

ip = sys.argv[1]

如果您想要正确的命令行处理,请查看argparse

如果您使用的是subprocess,则无需执行os.system。而不是:

ping = os.system("ping %s >nul" % str(ip))
ipVerification = subprocess.getoutput(str(ping))

你可以这样做:

ipVerification = subprocess.getoutput(f"ping {ip}")

ip = 55 这个ipVerification 的值是:

connect: Invalid argument

这将允许您在调用ip-api.com API 之前捕获它。或者,您可以忽略 ping,让来自 ip-api.com 的响应中的 message 确定 IP 地址是否有效。

if response.get("message") == "invalid query":
    print(f"The IP address is invalid: {ip}".
else:
    print(response.get("isp"))

【讨论】:

  • 当我这样做时,它会显示IndexError: list index out of range for ip = sys.argv[1]
  • 就像你说的那样,你做了ipinfo.py 55python ipinfo.py 55
  • 现在如果 IP 无效,它会显示{'message': 'invalid query', 'query': 'invalid_ip', 'status': 'fail'} Loading... P...,如果有效,它会提供信息,然后显示Loading... P...
  • 如果您在谈论 if response.get("message") == "invalid query": print(f"The IP address is invalid: {ip}". else: print(response.get("isp")),那么是的,我确实用它替换了我的 if 和 elif,但它给了我未定义响应的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
相关资源
最近更新 更多