0

I have the following text as log output (to the python console), when running a script agains the Qualys API and would like to parse the content of in the error message ("Error! Received A 4xx...)from there. I am using the qualysapi modul. Based on what I see in the module it creates an own logger instance and unfortunatelly the printed message is not contained in the raised exception by the module.

Any hints how such a thing would be possible?

      i     | Loading new data into Qualys...

Error! Received a 4XX client error or 5XX server error response.

Content = 
<!DOCTYPE SIMPLE_RETURN SYSTEM "<....>">
<SIMPLE_RETURN>
  <RESPONSE>
    <DATETIME>2021-11-20T17:07:40Z</DATETIME>
    <CODE>1905</CODE>
    <TEXT>parameter IPs has invalid value: One or more IPs are not assigned to this user: 10.73.32.133, 10.73.32.139-10.73.32.171, </TEXT>
  </RESPONSE>
</SIMPLE_RETURN>

      i     | Adding missing IPs to subscription, then we try again...

1 Answers1

1

You can redirect stdout or stderr to a file and then process the file:

import sys
sys.stdout = open('file', 'w')
print('test')
sys.stdout.close()

Check more about this here: Redirect stdout to a file in Python?

Dan Constantinescu
  • 1,426
  • 1
  • 7
  • 11
  • 1
    Thanks for pointing me in the right direction! I went with the contextlib option: https://stackoverflow.com/a/22434262/15953987 I still wonder if there is a better way, than simply opening a file during runtime – Gergo Peltz Nov 21 '21 at 08:37