diff --git a/load_check.py b/load_check.py index 9cf06ac19d09c8a2be9c674dc510714af2e4079e..7b429c7c4f1f27ed63840eaccb8b5ed535ba64d4 100644 --- a/load_check.py +++ b/load_check.py @@ -7,6 +7,8 @@ import unittest import random import os from datetime import datetime +import requests +import urllib3 from selenium import webdriver from selenium.webdriver.common.by import By @@ -26,6 +28,11 @@ class MultipleTest(unittest.TestCase): print("\n2. [필수 입력] 테스트 대상 웹 (e.g., https://example.com:8443/): ") self.dc_address = input() + if self.check_http_status(self.dc_address): + pass + else: + print("EMS 접속이 원할하지 않습니다. 프로그램을 종료합니다") + sys.exit(1) # A list is created to store multiple account configurations. self.account_configs = [] @@ -96,6 +103,53 @@ class MultipleTest(unittest.TestCase): os.makedirs(self.screenshots_folder, exist_ok=True) self.screenshot_base_dir = self.screenshots_folder + def check_http_status(self, url: str, max_retry: int = 3, ignore_cert: bool = True) -> bool: + """ + Checks the HTTP status of the given URL. + Follows redirects, retries up to `max_retry` times if the final status is not 200, + and optionally ignores SSL certificate validation. + + Args: + url (str): The target URL to check. + max_retry (int): Maximum number of retries if status is not 200. Defaults to 3. + ignore_cert (bool): If True, SSL certificate errors are ignored. Defaults to True. + + Returns: + bool: True if the final HTTP status is 200, otherwise False. + """ + + # Disable SSL certificate warnings (optional) + urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + + for attempt in range(1, max_retry + 1): + try: + # HEAD request first + response = requests.head( + url, allow_redirects=True, timeout=5, verify=not ignore_cert + ) + + # Some servers may not support HEAD properly + if response.status_code >= 400 or response.status_code == 405: + response = requests.get( + url, allow_redirects=True, timeout=5, verify=not ignore_cert + ) + + # print(f"Attempt {attempt}: {response.status_code} ({response.url})") + + # ✅ Success only if final status == 200 + if response.status_code == 200: + return True + + except requests.exceptions.RequestException as e: + print(f"Attempt {attempt}: Error - {e}") + + # Wait a bit before retrying + if attempt < max_retry: + time.sleep(1) + + # ❌ All retries failed or never got 200 + return False + def setUp(self): self._get_user_inputs() # Initialize a list to hold driver instances for each thread diff --git a/requirements.txt b/requirements.txt index 589f1a447d63caa0597c62160bfec48eb28b1b6d..8983552ca2ada042fb33e14f2fa404bf77784a5f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ selenium==4.36.0 -urllib3==2.5.0 \ No newline at end of file +urllib3==2.5.0 +requests==2.32.5 \ No newline at end of file