0

I have a Python function that pulls a JSON file, then reads this and pulls certain key pairs for inserting into a database.

def update_vuln_sets():
    try:
        logging.info('Getting latest vulnerability sets from Vulners API...')
        response = requests.get('https://vulners.com/api/v3/search/stats/')
        response.encoding = 'windows-1252'
        vuln_set = json.loads(response.text)
        vuln_type = vuln_set['data']['type_results']
    except Exception as e:
        logging.error(e)
    try:
        logging.info('Processing JSON response')
                for k in vuln_type:
        vuln_bulletinfamily = vuln_set['data']['type_results'][k]['bulletinFamily']
        vuln_name = vuln_set['data']['type_results'][k]['displayName']
        vuln_count = vuln_set['data']['type_results'][k]['count']
        try:
            logging.info('Connecting to the database...')
            con = MySQLdb.connect('5.57.62.97', 'vuln_backend', 'aHv5Gz50cNgR', 'vuln_backend')
            logging.info('Database connected!')
        except FileNotFoundError as fnf:
            logging.error(fnf)
        except MySQLdb.Error as e:
            logging.error(e)
        try:
            logging.info('Inserting vulnerability type ' + k + ' into DB')
            with con:
                cur = con.cursor()
                con.row_factory = MySQLdb.row
                cur.execute("INSERT OR IGNORE INTO vuln_sets (vulntype, displayname, bulletinfamily, vulncount)"
                            " values (?,?,?,?)", (k, vuln_name, vuln_bulletinfamily, vuln_count))
                con.commit()
            logging.info('Vulnerability type ' + k + ' inserted successfully!')
            except Exception as e:
                logging.error('Vulnerability type ' + k + 'not inserted! - Error: ' + e)
        logging.info('Vulnerability sets successfully updated!')
    except Exception as e:
        logging.error(e)

Checking through my logging it is getting held up at the Inserting Record stage but it just gives an error of root ERROR must be str, not AttributeError

2
  • 1
    Please post the full traceback. That error looks weird. Commented Oct 23, 2017 at 15:28
  • have you tried printing out (logging) the requests before executing them ? Sound like one of the objects you're manipulating is an Error instead of a string (maybe you're accessing an empty structure or something) Commented Oct 23, 2017 at 15:29

1 Answer 1

1

You're logging e which is not a str it is an AttributeError. Based on the bit of a stack trace you shared, it looks like 'logging.error()takes astr` argument.

You could either change logging.error() to accept an Exception instead of a string, or you could call take a string portion of the error to log it.

Sign up to request clarification or add additional context in comments.

4 Comments

So a fix would be logging.error(str(e)) I guess? But what would be a better way to do it?
You could figure out what string part of e you want to log and write a function that extracts that part.
I'm not the OP, but your answer feels incomplete.
That sorted it, i'm now getting module 'MySQLdb' has no attribute 'row'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.