user
Design a Python script that reads a text file containing a list of URLs, checks their availability by sending HTTP requests, and logs the response time and status code for each URL. PLAINFORMAT
assistant
import requests
from time import time

def check_url_availability(url):
    start_time = time()
    try:
        response = requests.get(url)
        status_code = response.status_code
    except Exception as e:
        status_code = "Error"
    end_time = time()
    response_time = round(end_time - start_time, 2)
    return status_code, response_time

with open("urls.txt", "r") as file:
    urls = file.readlines()

with open("log.txt", "w") as log_file:
    for url in urls:
        url = url.strip()
        status_code, response_time = check_url_availability(url)
        log_file.write(f"{url} | Status: {status_code} | Response Time: {response_time}s\n")

print("URL availability check completed. Check the log.txt file for results.")