30. 7. 2021

Available WEB sites checker

  • Add button code to python file

Python script checker

'''
This script detect if the website reacting correctly.

python webDetection --WebSite <web> --ResponseNumber 200 --TimeoutForTextDetection 15 --SearchStirng "/html" --Repeating 4
'''
# Include standard modules
# parameters:
# - weburl
# - responseNumber 100-500
# - timeForAcceptError (s)
# - stringForChecking ""

import requests
import argparse
import genReportHtml

def repeateAction(url, timeoutAction, retries):
    i = 0
    while i < retries:
        response = getResponse(url, timeoutAction)
        if hasattr(response, 'status_code'):
            return response
        else:
            i += 1
            print(f'{i}. retries')
    return False

def getResponse(url, timeoutAction):
    try:
        response = requests.get(url, timeout=timeoutAction)
    except Exception as e:
        return f"NOT OK: {str(e)}"
    else:
        return response


def parsingStringFrom(response, string):
    result = response.find(string)
    if result == -1:
        return False
    else:
        return True


def avalibleDetection(url, response_number, timeoutForErrorString, string, retries):
    content = repeateAction(url, timeoutForErrorString, retries)
    # print(conn);
    try:
        if int(response_number) == content.status_code:
            print(f'SUCCESS: The response number is {str(content.status_code)}')
            if parsingStringFrom(content.text, string):
                return True
            else:
                return False
        else:
            print(f'ERROR: The response number is not {str(content.status_code)}')
    except AttributeError:
        print(f'ERROR: {AttributeError}')
        return False


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--WebSite', required=True, help="WebSite specify which web site tested.")
    parser.add_argument('--ResponseNumber', required=True,
                        help="ResponseNumber which number shlould expect for passing test.")
    parser.add_argument('--TimeoutForAction', required=True,
                        help="TimeoutForAction delay for provide data for text detection on web site.")
    parser.add_argument('--SearchString', required=True, help="Search string which can be contains the website")
    parser.add_argument('--Repeating', required=False, help="How many retries will allows.")

    args = parser.parse_args()

    if str(args.WebSite) and int(args.ResponseNumber) and int(args.TimeoutForAction) and str(args.SearchString) and int(args.Repeating):
        if avalibleDetection(str(args.WebSite), int(args.ResponseNumber), int(args.TimeoutForAction),
                             str(args.SearchString), int(args.Repeating)):
            genReportHtml.genReport('reports', 'report.log', str(args.WebSite) + '~Connected"')
            print(f"SUCCESS: Web address: {args.WebSite} is connected")
        else:
            genReportHtml.genReport('reports', 'report.log', str(args.WebSite) + '~notConnected"')
            raise Exception(f"ERROR: Web address: {str(args.WebSite)} is NOT connected")
    else:
        raise Exception('The parameters required')
  • Run script
python <fileName> --WebSite <web> --ResponseNumber 200 --TimeoutForTextDetection 15 --SearchStirng "/html" --Repeating 4
Share