Внезапно в журнале появляются периодические ошибки, например: :
Код: Выделить всё
Downloaded 6000 CVEs.
Process finished at 2024-07-02 11:02:55
Total duration: 173.34 seconds
Downloaded 8000 CVEs.
Error: Invalid API key
Process finished at 2024-07-02 15:01:10
Total duration: 0.30 seconds
Downloaded 0 CVEs.
Process finished at 2024-07-02 17:00:37
Total duration: 1.92 seconds
Downloaded 0 CVEs.
Error: Invalid API key
Код: Выделить всё
import requests
import logging
import time
# Configure logging
logging.basicConfig(filename='//api_key_check.log', level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')
def is_valid_api_key(apiKey, retries=3, delay=5, timeout=10):
test_url = 'https://services.nvd.nist.gov/rest/json/cves/2.0'
test_params = {'startIndex': 0, 'resultsPerPage': 1}
headers = {'apiKey': apiKey}
for attempt in range(retries):
try:
logging.info(f"Attempt {attempt + 1} to check API key.")
test_response = requests.get(test_url, params=test_params, headers=headers, timeout=timeout)
if test_response.status_code == 200:
logging.info("API key is valid.")
return True
elif test_response.status_code == 403:
logging.error("Forbidden: The API key might be invalid or rate-limited.")
return False
else:
logging.error(f"Unexpected status code {test_response.status_code} on attempt {attempt + 1}")
except requests.Timeout:
logging.error(f"Request timed out on attempt {attempt + 1}")
except requests.RequestException as e:
logging.error(f"Request failed on attempt {attempt + 1}: {e}")
time.sleep(delay)
logging.error("API key validation failed after multiple attempts.")
return False
# Replace 'your_api_key_here' with your actual API key
apiKey = 'your_api_key_here'
if is_valid_api_key(apiKey):
print("API key is valid.")
else:
print("Error: Invalid API key.")
Код: Выделить всё
Error: Invalid API key.
У них есть это в их чат-группе:
https://groups.google.com/a/ list.nist.gov/g/nvd-news
Кто-нибудь сталкивался с этой проблемой или имеет какие-либо рекомендации?
Подробнее здесь: https://stackoverflow.com/questions/786 ... -0-changes