【问题标题】:How to return to earlier (if statement) loop in python如何在 python 中返回到较早的(if 语句)循环
【发布时间】:2022-11-16 05:43:12
【问题描述】:

我想在我的脚本中编写一个简单的函数(我是初学者)来检查和测试来自 VirusTotal 的用户 API 密钥。

那是我的想法:

首先,我想检查用户是否在代码或字段中键入他的 API KEY 为空。

其次,我想检查 API KEY 是否正确。我不知道如何以最简单的方式检查它,所以我使用我在 VirusTotal 上找到的最简单的查询并检查响应代码是否为 200。

但是当 API 密钥字段为空并且用户输入错误的 API 密钥时,我遇到了问题。之后,我的功能结束。我想回到之前的 if 条件,检查这次 api key 是否正确。

当用户输入正确的 API KEY 时,函数打印正确的消息。

这是我的代码:

import requests
import json

def auth_vt_apikey():
    """This function test VirusTotal's Api Key"""

api_key = ''

if api_key == '':
    api_key = str(input("Please enter your VirusTotal's API Key: "))
else:
    None

url = 'https://www.virustotal.com/vtapi/v2/url/report'
params = {'apikey': api_key}
response = requests.get(url, params=params)

if response.status_code == 200:
    print('Your Api Key is correct')
else:
    api_key = str(input("Your Api Key is incorrect. Please re-type your Api Key: "))
        
auth_vt_apikey()

您能向我解释一下我在这里做错了什么以及值得补充的地方吗?我也将感谢指南的链接,这样我就可以在这个例子中自学。

【问题讨论】:

  • 你需要一个循环。
  • 到底什么是if语句循环
  • 您可以只使用 while 循环。网上应该有很多材料解释 Python 中的循环。
  • 不清楚哪些代码在你的函数中,哪些不在。请更新代码的缩进。 Python 对缩进非常敏感,python 程序员也是如此。

标签: python api if-statement


【解决方案1】:

我想你想实现这个:

import requests
import json

def auth_vt_apikey():
    """This function test VirusTotal's Api Key"""
    url = 'https://www.virustotal.com/vtapi/v2/url/report'
    api_key = ''
    msg = "Please enter your VirusTotal's API Key: "

    while api_key == '':
        api_key = str(input(msg))
    
        params = {'apikey': api_key}
        response = requests.get(url, params=params)

        if response.status_code == 200:
            print('Your Api Key is correct')
        else:
            api_key = ''
            msg = "Your Api Key is incorrect. Please re-type your Api Key: "
        
auth_vt_apikey()

【讨论】:

  • 感谢帮助!我明白我的错误。感谢您提供简单的解决方案:)
【解决方案2】:

您可以使用 while 循环,如下所示:

import requests
import json

def auth_vt_apikey():
    """This function test VirusTotal's Api Key"""
    ...


api_key = ''
api_key_valid = False
while (not api_key_valid):
    if api_key == '':
        api_key = str(input("Please enter your VirusTotal's API Key: "))

    url = 'https://www.virustotal.com/vtapi/v2/url/report'
    params = {'apikey': api_key}
    response = requests.get(url, params={'apikey': api_key})

    if response.status_code == 200:
        print('Your Api Key is correct')
        api_key_valid = True
    else:
        print("Your Api Key is incorrect.", end=" ")
        
auth_vt_apikey()

【讨论】:

    【解决方案3】:

    首先:所有应该在你的函数中的代码都需要缩进。

    在请求 API 密钥的初始代码中:

    api_key = ''
    
    if api_key == '':
        api_key = str(input("Please enter your VirusTotal's API Key: "))
    else:
        None
    

    if api_key == '' 是完全没有必要的(因为你刚刚设置了api_key = '',所以它总是正确的),str() 围绕input() 也是如此(它总是返回一个str)。做就是了:

    api_key = input("Please enter your VirusTotal's API Key: ")
    

    当您请求测试 API 密钥时:

    response = requests.get(url, params=params)
    
    if response.status_code == 200:
        print('Your Api Key is correct')
    else:
        api_key = str(input("Your Api Key is incorrect. Please re-type your Api Key: "))
    

    如果您想实际使用新密钥重试,您应该循环执行此操作:

    while True:
        response = requests.get(url, params={'apikey': api_key})
    
        if response.status_code == 200:
            print('Your Api Key is correct')
            return api_key  # ends the loop and returns the valid key to the caller
    
        api_key = input("Your Api Key is incorrect. Please re-type your Api Key: ")
        # loop continues until the API key is correct
    

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多