I want to check these endpoints if the answer is other than 0 it is ok but if 0 it is not ok. After I have done it these checks, I want to send the result to an 'outlook' email, can anyone help me with it.
This is my code so far.
import json, requests, urllib.parse, re
from termcolor import colored
import numpy as np
cdwenv1 = 'cdwu' #Note that it only works with the http version right now
cdwenv2 = 'cdwp'
#Dev Static
cdwenv = '' #leave empty
# static
cdwEndPoints = [
'http://cdwu/cdw/counterparties?count=true'
,'http://cdwu/identifier/assets?count=true'
,'http://cdwu/identifier/assetListings?count=true'
]
# Check that Counts are different from 0
import sys
def countsCDWdata(input: list, cdwenv1, flag=0):
results = []
for i, item in enumerate(input):
result = []
if '?' in item:
cdwcounturl = item
try:
r = requests.get(cdwcounturl)
except:
print(cdwcounturl, colored('is erroring', 'red'))
result.append([cdwenv1, r.text])
sys.stdout.flush()
sys.stdout.write('\r Queried '+ str(i) + ' out of ' + str(len(input)))
results.append([re.findall('//.*?/.*?/(.*?)/',item)[0], result[0], result[0]])
if flag == 1:
return(results)
df = pd.DataFrame(results, columns = ['CDWEndpoint', 'CDW Env', 'Counts'])
df['Status'] = np.where(df['Counts1'] != 0, colored('Ok', 'green'), colored('not Ok', 'red'))
sys.stdout.flush()
sys.stdout.write('\r')
print(df)
I am getting this error:
http://cdwu/cdw/counterparties?count=true is erroring
---------------------------------------------------------------------------
http://cdwu/cdw/counterparties?count=true is erroring
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-72-797620017bbc> in <module>()
----> 1 countsCDWdata(cdwEndPoints, [cdwenv1])
<ipython-input-70-73176a33caf9> in countsCDWdata(input, cdwenv1, flag)
9 except:
10 print(cdwcounturl, colored('is erroring', 'red'))
---> 11 result.append([cdwenv1, r.text])
12 sys.stdout.flush()
13 sys.stdout.write('\r Queried '+ str(i) + ' out of ' + str(len(input)))
UnboundLocalError: local variable 'r' referenced before assignment
rvariable is only assigned line 12 => You don't achieve the try, fall in the except =>rkind of "doesn't exist". Which lead to this error. And look at the first line of the errorhttp://cdwu/cdw/counterparties?count=true is erroring, it looks like the error printing line 14.