Spaces:
Sleeping
Sleeping
File size: 2,596 Bytes
6dc7be1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# 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()
|