如何使用Python查询VirusTotal API密钥的配额使用情况
Python 查询 VirusTotal API 配额使用方案
你可以直接调用VirusTotal提供的用户信息接口实现脚本内实时查询配额,无需登录网页端,具体实现方式如下:
前置依赖
需要先安装Python的HTTP请求库requests,安装命令:pip install requests
实现示例代码
import requests # 替换为你自己的VirusTotal API密钥 API_KEY = "你的API密钥" def get_vt_quota(api_key): url = "https://www.virustotal.com/api/v3/users/{}".format(api_key) headers = { "accept": "application/json", "x-apikey": api_key } response = requests.get(url, headers=headers) if response.status_code == 200: data = response.json() quotas = data["data"]["attributes"]["quotas"] # 提取常用的日/月请求配额,可根据需要扩展其他字段 daily_used = quotas["api_requests_daily"]["used"] daily_allowed = quotas["api_requests_daily"]["allowed"] monthly_used = quotas["api_requests_monthly"]["used"] monthly_allowed = quotas["api_requests_monthly"]["allowed"] return { "daily_used": daily_used, "daily_total": daily_allowed, "daily_remaining": daily_allowed - daily_used, "monthly_used": monthly_used, "monthly_total": monthly_allowed, "monthly_remaining": monthly_allowed - monthly_used } else: raise Exception(f"查询配额失败,状态码:{response.status_code},错误信息:{response.text}") # 调用示例 if __name__ == "__main__": quota_info = get_vt_quota(API_KEY) print("当前配额使用情况:") print(f"日配额:已用{quota_info['daily_used']},剩余{quota_info['daily_remaining']},总计{quota_info['daily_total']}") print(f"月配额:已用{quota_info['monthly_used']},剩余{quota_info['monthly_remaining']},总计{quota_info['monthly_total']}")
注意事项
- 配额查询接口本身不会消耗你的API请求配额,可以放心频繁调用
- 如果你使用的是企业级付费API,返回结果中还会包含文件扫描、URL扫描等细分配额项,按需提取对应字段即可
- 可以将该函数嵌入你的业务逻辑中,在配额剩余不足时自动触发暂停、告警等逻辑,避免脚本运行中断
内容的提问来源于stack exchange,提问作者Joanna
相关产品推荐
相关产品推荐

