【发布时间】: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