Commit ba841876 authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Merge branch 'noc-py3-fix-whois' into 'master'

py3: Fix whois client

See merge request noc/noc!3258

(cherry picked from commit 5082b2b2)

bffb9707 py3: Fix whois client
43a6d99e Fix
parent 2da9f227
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Whois client # Whois client
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project # Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details # See LICENSE for details
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -12,10 +12,11 @@ import logging ...@@ -12,10 +12,11 @@ import logging
# Third-party modules # Third-party modules
from tornado.tcpclient import TCPClient from tornado.tcpclient import TCPClient
import tornado.gen import tornado.gen
import tornado.ioloop from tornado.ioloop import IOLoop
# NOC modules # NOC modules
from noc.core.validators import is_fqdn from noc.core.validators import is_fqdn
from noc.core.comp import smart_bytes, smart_text
DEFAULT_WHOIS_SERVER = "whois.ripe.net" DEFAULT_WHOIS_SERVER = "whois.ripe.net"
DEFAULT_WHOIS_PORT = 43 DEFAULT_WHOIS_PORT = 43
...@@ -23,7 +24,7 @@ DEFAULT_TIMEOUT = 60 ...@@ -23,7 +24,7 @@ DEFAULT_TIMEOUT = 60
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
VERISIGN_MAP = { FIELDS_MAP = {
"domain name": "domain", "domain name": "domain",
"name server": "nserver", "name server": "nserver",
"creation date": "created", "creation date": "created",
...@@ -31,34 +32,24 @@ VERISIGN_MAP = { ...@@ -31,34 +32,24 @@ VERISIGN_MAP = {
} }
def parse_verisign(data): def parse_response(data):
""" """
Parse whois response from verisign Parse whois response
:param data: :param data:
:return: :return:
""" """
r = [] r = []
for line in data.splitlines(): for line in data.splitlines():
if not line.startswith(" "):
continue
line = line.strip() line = line.strip()
if line.startswith(">>>"):
break
k, v = line.split(":", 1) k, v = line.split(":", 1)
k = k.strip().lower() k = k.strip().lower()
k = VERISIGN_MAP.get(k, k) k = FIELDS_MAP.get(k, k)
r += [(k, v.strip())] r += [(k, v.strip())]
return r return r
def parse_response(data):
"""
Parse whois response
:param data:
:return:
"""
if data.startswith(" ") and ">>> Last update of whois database" in data:
return parse_verisign(data)
@tornado.gen.coroutine @tornado.gen.coroutine
def whois_async(query, fields=None): def whois_async(query, fields=None):
""" """
...@@ -83,10 +74,11 @@ def whois_async(query, fields=None): ...@@ -83,10 +74,11 @@ def whois_async(query, fields=None):
logger.error("Cannot resolve host '%s': %s", server, e) logger.error("Cannot resolve host '%s': %s", server, e)
raise tornado.gen.Return() raise tornado.gen.Return()
try: try:
yield stream.write(str(query) + "\r\n") yield stream.write(smart_bytes(query) + b"\r\n")
data = yield stream.read_until_close() data = yield stream.read_until_close()
finally: finally:
yield stream.close() yield stream.close()
data = smart_text(data)
data = parse_response(data) data = parse_response(data)
if fields: if fields:
data = [(k, v) for k, v in data if k in fields] data = [(k, v) for k, v in data if k in fields]
...@@ -99,7 +91,6 @@ def whois(query, fields=None): ...@@ -99,7 +91,6 @@ def whois(query, fields=None):
result = yield whois_async(query, fields) result = yield whois_async(query, fields)
r.append(result) r.append(result)
ioloop = tornado.ioloop.IOLoop.instance()
r = [] r = []
ioloop.run_sync(_whois) IOLoop().run_sync(_whois)
return r[0] return r[0]
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# whois client test
# ----------------------------------------------------------------------
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
import pytest
# NOC modules
from noc.core.ioloop.whois import whois
@pytest.mark.parametrize("domain", ["getnoc.com", "nocproject.org"])
def test_whois_domain(domain):
data = dict(whois(domain))
assert "domain" in data
assert data["domain"].lower() == domain.lower()
assert "paid-till" in data
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment