posted by 구로공돌이 2025. 4. 8. 09:38

import requests
import time
from datetime import datetime

URL = "https://example.com"  # 측정할 대상 URL
LOG_FILE = "response_time_log.txt"  # 로그 파일 경로

def measure_response_time(url):
    try:
        start = time.time()
        response = requests.get(url, timeout=10)
        end = time.time()

        duration_ms = round((end - start) * 1000, 2)
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        log_line = f"{timestamp} | {url} | {response.status_code} | {duration_ms}ms"
        print(log_line)

        with open(LOG_FILE, "a") as f:
            f.write(log_line + "\n")

    except requests.exceptions.RequestException as e:
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        log_line = f"{timestamp} | {url} | ERROR | {str(e)}"
        print(log_line)
        with open(LOG_FILE, "a") as f:
            f.write(log_line + "\n")

if __name__ == "__main__":
    while True:
        measure_response_time(URL)
        time.sleep(60)  # 1분 대기