Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
def check_domains(domain_text):
|
| 7 |
+
"""
|
| 8 |
+
Takes a string of domains (one per line), checks each one,
|
| 9 |
+
and returns a formatted string with the results.
|
| 10 |
+
"""
|
| 11 |
+
if not domain_text:
|
| 12 |
+
return "Please enter at least one domain."
|
| 13 |
+
|
| 14 |
+
domains = [d.strip() for d in domain_text.split('\n') if d.strip()]
|
| 15 |
+
results = []
|
| 16 |
+
|
| 17 |
+
for domain in domains:
|
| 18 |
+
# Prepend http:// if no scheme is present, as requests needs it.
|
| 19 |
+
if not domain.startswith(('http://', 'https://')):
|
| 20 |
+
url = f"http://{domain}"
|
| 21 |
+
else:
|
| 22 |
+
url = domain
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
start_time = time.time()
|
| 26 |
+
# We use a timeout to prevent the app from hanging on unresponsive domains.
|
| 27 |
+
response = requests.get(url, timeout=10, allow_redirects=True)
|
| 28 |
+
end_time = time.time()
|
| 29 |
+
|
| 30 |
+
response_time = round((end_time - start_time) * 1000) # Convert to milliseconds
|
| 31 |
+
status_code = response.status_code
|
| 32 |
+
|
| 33 |
+
# Check for a successful status code (2xx or 3xx for redirects)
|
| 34 |
+
if 200 <= status_code < 400:
|
| 35 |
+
results.append(f"✅ **{domain}** - `Status: {status_code}` - `Response Time: {response_time} ms`")
|
| 36 |
+
else:
|
| 37 |
+
results.append(f"⚠️ **{domain}** - `Status: {status_code}` - `Response Time: {response_time} ms` (Client/Server Error)")
|
| 38 |
+
|
| 39 |
+
except requests.exceptions.Timeout:
|
| 40 |
+
results.append(f"❌ **{domain}** - `Error: Request Timed Out`")
|
| 41 |
+
except requests.exceptions.RequestException as e:
|
| 42 |
+
results.append(f"❌ **{domain}** - `Error: Connection Failed` (Could not resolve host or other network issue)")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
results.append(f"❌ **{domain}** - `An unexpected error occurred: {e}`")
|
| 45 |
+
|
| 46 |
+
# Join the results into a single string with Markdown for formatting.
|
| 47 |
+
return "\n\n".join(results)
|
| 48 |
+
|
| 49 |
+
# --- Gradio Interface Definition ---
|
| 50 |
+
# This creates the web UI for our function.
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=check_domains,
|
| 53 |
+
inputs=gr.Textbox(
|
| 54 |
+
lines=10,
|
| 55 |
+
placeholder="Enter domains, one per line...\ngoogle.com\nwhatsapp.com\nnonexistent-domain-12345.com",
|
| 56 |
+
label="Domains to Check"
|
| 57 |
+
),
|
| 58 |
+
outputs=gr.Markdown(label="Results"),
|
| 59 |
+
title="Domain Network Response Checker",
|
| 60 |
+
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."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Launch the web application
|
| 64 |
+
interface.launch()
|