Cd / app.py
understanding's picture
Create app.py
6dc7be1 verified
# app.py
import gradio as gr
import requests
import time
def check_domains(domain_text):
"""
Takes a string of domains (one per line), checks each one,
and returns a formatted string with the results.
"""
if not domain_text:
return "Please enter at least one domain."
domains = [d.strip() for d in domain_text.split('\n') if d.strip()]
results = []
for domain in domains:
# Prepend http:// if no scheme is present, as requests needs it.
if not domain.startswith(('http://', 'https://')):
url = f"http://{domain}"
else:
url = domain
try:
start_time = time.time()
# We use a timeout to prevent the app from hanging on unresponsive domains.
response = requests.get(url, timeout=10, allow_redirects=True)
end_time = time.time()
response_time = round((end_time - start_time) * 1000) # Convert to milliseconds
status_code = response.status_code
# Check for a successful status code (2xx or 3xx for redirects)
if 200 <= status_code < 400:
results.append(f"βœ… **{domain}** - `Status: {status_code}` - `Response Time: {response_time} ms`")
else:
results.append(f"⚠️ **{domain}** - `Status: {status_code}` - `Response Time: {response_time} ms` (Client/Server Error)")
except requests.exceptions.Timeout:
results.append(f"❌ **{domain}** - `Error: Request Timed Out`")
except requests.exceptions.RequestException as e:
results.append(f"❌ **{domain}** - `Error: Connection Failed` (Could not resolve host or other network issue)")
except Exception as e:
results.append(f"❌ **{domain}** - `An unexpected error occurred: {e}`")
# Join the results into a single string with Markdown for formatting.
return "\n\n".join(results)
# --- Gradio Interface Definition ---
# This creates the web UI for our function.
interface = gr.Interface(
fn=check_domains,
inputs=gr.Textbox(
lines=10,
placeholder="Enter domains, one per line...\ngoogle.com\nwhatsapp.com\nnonexistent-domain-12345.com",
label="Domains to Check"
),
outputs=gr.Markdown(label="Results"),
title="Domain Network Response Checker",
description="Enter one or more domain names below to check their HTTP status and response time from this Hugging Face Space. The check includes a 10-second timeout."
)
# Launch the web application
interface.launch()