diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9af213 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.project +.pydevproject diff --git a/dnsdb_query.py b/dnsdb_query.py index d8f3fdc..445e1c3 100755 --- a/dnsdb_query.py +++ b/dnsdb_query.py @@ -20,7 +20,7 @@ import os import sys import time -import urllib2 +import requests from cStringIO import StringIO try: @@ -37,11 +37,12 @@ locale.setlocale(locale.LC_ALL, '') class DnsdbClient(object): - def __init__(self, server, apikey, limit=None, json=False): + def __init__(self, server, apikey, limit=None, json=False, verify=False): self.server = server self.apikey = apikey self.limit = limit self.json = json + self.verify = verify def query_rrset(self, oname, rrtype=None, bailiwick=None): if bailiwick: @@ -68,23 +69,23 @@ def query_rdata_ip(self, rdata_ip): def _query(self, path): res = [] url = '%s/lookup/%s' % (self.server, path) + headers = {'Accept': 'application/json', 'X-Api-Key': self.apikey} if self.limit: - url += '?limit=%d' % self.limit - req = urllib2.Request(url) - req.add_header('Accept', 'application/json') - req.add_header('X-Api-Key', self.apikey) - try: - http = urllib2.urlopen(req) - while True: - line = http.readline() + params = '?limit=%d' % self.limit + else: + params = "" + response = requests.get(url, params=params, headers=headers, verify=self.verify) + if response.status_code != 200: + sys.stderr.write(str(response.status_code) + " " + response.reason + "\n") + return res + else: + for line in response.iter_lines(): if not line: break if self.json: res.append(line) else: res.append(json.loads(line)) - except urllib2.HTTPError, e: - sys.stderr.write(str(e) + '\n') return res def sec_to_text(ts): @@ -219,8 +220,16 @@ def main(): if not 'APIKEY' in cfg: sys.stderr.write('dnsdb_query: APIKEY not defined in config file\n') sys.exit(1) - - client = DnsdbClient(cfg['DNSDB_SERVER'], cfg['APIKEY'], options.limit, options.json) + if not 'VERIFY' in cfg: + cfg['VERIFY'] = False + elif cfg['VERIFY'].lower() in ['true', '1', 't', 'y', 'yes']: + cfg['VERIFY'] = True + if 'CA_BUNDLE' in cfg: + cfg['VERIFY'] = cfg['CA_BUNDLE'] + else: + cfg['VERIFY'] = False + + client = DnsdbClient(cfg['DNSDB_SERVER'], cfg['APIKEY'], options.limit, options.json, cfg['VERIFY']) if options.rrset: res_list = client.query_rrset(*options.rrset.split('/')) fmt_func = rrset_to_text