mirror of
https://github.com/cve-search/PyVulnerabilityLookup.git
synced 2024-11-25 16:27:23 +00:00
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
from importlib.metadata import version
|
||
|
from pathlib import Path
|
||
|
from typing import Dict, Optional, Any
|
||
|
from urllib.parse import urljoin, urlparse
|
||
|
|
||
|
import requests
|
||
|
|
||
|
|
||
|
class PyVulnerabilityLookup():
|
||
|
|
||
|
def __init__(self, root_url: str, useragent: Optional[str]=None):
|
||
|
'''Query a specific instance.
|
||
|
|
||
|
:param root_url: URL of the instance to query.
|
||
|
'''
|
||
|
self.root_url = root_url
|
||
|
|
||
|
if not urlparse(self.root_url).scheme:
|
||
|
self.root_url = 'http://' + self.root_url
|
||
|
if not self.root_url.endswith('/'):
|
||
|
self.root_url += '/'
|
||
|
self.session = requests.session()
|
||
|
self.session.headers['user-agent'] = useragent if useragent else f'PyProject / {version("pyvulnerabilitylookup")}'
|
||
|
|
||
|
@property
|
||
|
def is_up(self) -> bool:
|
||
|
'''Test if the given instance is accessible'''
|
||
|
try:
|
||
|
r = self.session.head(self.root_url)
|
||
|
except requests.exceptions.ConnectionError:
|
||
|
return False
|
||
|
return r.status_code == 200
|
||
|
|
||
|
def redis_up(self) -> Dict:
|
||
|
'''Check if redis is up and running'''
|
||
|
r = self.session.get(urljoin(self.root_url, 'redis_up'))
|
||
|
return r.json()
|
||
|
|
||
|
def get_vulnerability(self, vulnerability_id: str) -> Dict[str, Any]:
|
||
|
r = self.session.get(urljoin(self.root_url, str(Path('vulnerability', vulnerability_id))))
|
||
|
return r.json()
|