Ejemplo n.º 1
0
def save_whois(domain, whois_obj):
    logging.info("Saving whois(domain: '%s') to file..." % domain)
    original_domain_str = domain
    #: 反向拆解域名,构造 whois 文件存放目录
    domain = domain.split('.')
    domain.reverse()
    try:
        path = os.path.join(WHOIS_DIR, *domain)
        if not os.path.exists(path):
            os.makedirs(path)

        # 文件形式保存 whois 原始信息
        absolute_path = os.path.join(path, WHOIS_FILENAME)
        with open(absolute_path, 'w') as whois_file:
            whois_file.write(whois_obj.text)
    except Exception as e:
        logging.error("Saving to file Exception: %s" % e)

    # 然后以 Json 形式保存到数据库
    try:
        with DB_CONN.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `whois` (`domain`, `whois`, `last_modified`) VALUES (%s, %s, %s)"
            cursor.execute(sql, (original_domain_str,
                                 json.dumps(json.loads(str(whois_obj)),
                                            ensure_ascii=False,
                                            separators=(',', ':')),
                                 time.strftime('%Y-%m-%d %H:%M:%S')))
        # connection is not autocommit by default. So you must commit to save your changes.
        DB_CONN.commit()
    except AttributeError as ae:
        logging.error("DB connection timeout. Exception: %s" % ae.args)
        DB_CONN.connect()
    except Exception as e:
        logging.error("Save into SQL error. Exception: %s" % e.args)

    try:
        with DB_CONN.cursor() as cursor:
            # Read a single record
            sql = "SELECT `id`, `domain` FROM `whois` WHERE `domain`=%s"
            cursor.execute(sql, (original_domain_str, ))
            result = cursor.fetchone()
            logging.debug("Saved whois json into mysql: %s" % result)
    except AttributeError as ae:
        logging.error("DB connection timeout. Exception: %s" % ae.args)
        DB_CONN.connect()
    except Exception as e:
        logging.error("Query SQL error. Exception: %s" % e.args)
Ejemplo n.º 2
0
def save_whois(domain, whois_obj):
    logging.info("Saving whois(domain: '%s') to file..." % domain)
    original_domain_str = domain
    #: 反向拆解域名,构造 whois 文件存放目录
    domain = domain.split('.')
    domain.reverse()
    try:
        path = os.path.join(WHOIS_DIR, *domain)
        if not os.path.exists(path):
            os.makedirs(path)

        # 文件形式保存 whois 原始信息
        absolute_path = os.path.join(path, WHOIS_FILENAME)
        with open(absolute_path, 'w') as whois_file:
            whois_file.write(whois_obj.text)
    except Exception as e:
        logging.error("Saving to file Exception: %s" % e)

    # 然后以 Json 形式保存到数据库
    try:
        with DB_CONN.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `whois` (`domain`, `whois`, `last_modified`) VALUES (%s, %s, %s)"
            cursor.execute(sql, (original_domain_str,
                                 json.dumps(json.loads(str(whois_obj)), ensure_ascii=False, separators=(',', ':')),
                                 time.strftime('%Y-%m-%d %H:%M:%S')))
        # connection is not autocommit by default. So you must commit to save your changes.
        DB_CONN.commit()
    except AttributeError as ae:
        logging.error("DB connection timeout. Exception: %s" % ae.args)
        DB_CONN.connect()
    except Exception as e:
        logging.error("Save into SQL error. Exception: %s" % e.args)

    try:
        with DB_CONN.cursor() as cursor:
            # Read a single record
            sql = "SELECT `id`, `domain` FROM `whois` WHERE `domain`=%s"
            cursor.execute(sql, (original_domain_str,))
            result = cursor.fetchone()
            logging.debug("Saved whois json into mysql: %s" % result)
    except AttributeError as ae:
        logging.error("DB connection timeout. Exception: %s" % ae.args)
        DB_CONN.connect()
    except Exception as e:
        logging.error("Query SQL error. Exception: %s" % e.args)