import pandas as pd import requests import gradio as gr import time import tempfile def check_urls(url_list): urls = url_list.strip().split('\n') status_codes = [] for url in urls: try: final_status_code = None current_url = url while True: response = requests.get(current_url, allow_redirects=False) status_code = response.status_code if status_code == 200 or status_code == 404: final_status_code = status_code break elif 300 <= status_code < 400: current_url = response.headers.get('Location') if not current_url: final_status_code = status_code break else: final_status_code = status_code break time.sleep(1) # Wait for 1 second between requests status_codes.append(final_status_code) except requests.RequestException: status_codes.append(float('nan')) # Handle connection errors df = pd.DataFrame({"URL": urls, "Status Code": status_codes}) return df def apply_style(code): if pd.isna(code): return "background-color: yellow" elif code == 200: return "background-color: green" elif code == 404: return "background-color: red" else: return "background-color: orange" def style_df(df): return df.style.applymap(apply_style, subset=["Status Code"]) def to_csv(df): with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp: df.to_csv(tmp.name, index=False) return tmp.name with gr.Blocks() as demo: url_input = gr.Textbox(lines=10, placeholder="Enter URLs separated by new lines...") output_df = gr.DataFrame() download_button = gr.File(label="Download CSV") def process_and_return(url_list): df = check_urls(url_list) styled_df = style_df(df) csv_file = to_csv(df) return styled_df, csv_file url_input.change(process_and_return, inputs=url_input, outputs=[output_df, download_button]) demo.launch()