Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| import socket | |
| def fetch_url(url): | |
| try: | |
| r = requests.get(url, timeout=5) | |
| status = str(r.status_code) | |
| headers = str(dict(r.headers)) | |
| body = r.text[:2000] | |
| return "Status: " + status + "\nHeaders: " + headers + "\nBody:\n" + body | |
| except Exception as e: | |
| return "Error: " + str(e) | |
| def get_env(): | |
| lines = [] | |
| for k, v in sorted(os.environ.items()): | |
| lines.append(k + "=" + v[:100]) | |
| return "\n".join(lines) | |
| def net_info(): | |
| hostname = socket.gethostname() | |
| try: | |
| ip = socket.gethostbyname(hostname) | |
| except Exception: | |
| ip = "unknown" | |
| parts = ["Hostname: " + hostname, "IP: " + ip] | |
| try: | |
| with open("/etc/resolv.conf") as f: | |
| parts.append("resolv.conf:\n" + f.read()) | |
| except Exception: | |
| pass | |
| try: | |
| with open("/etc/hosts") as f: | |
| parts.append("hosts:\n" + f.read()) | |
| except Exception: | |
| pass | |
| return "\n".join(parts) | |
| def read_file(path): | |
| try: | |
| with open(path, 'r') as f: | |
| return f.read()[:5000] | |
| except Exception as e: | |
| return 'Error: ' + str(e) | |
| with gr.Blocks() as demo: | |
| with gr.Tab("Fetch"): | |
| url_in = gr.Textbox(label="url") | |
| url_out = gr.Textbox(label="output") | |
| url_in.submit(fetch_url, url_in, url_out) | |
| with gr.Tab("Env"): | |
| env_btn = gr.Button("Get Env") | |
| env_out = gr.Textbox(label="env") | |
| env_btn.click(get_env, None, env_out) | |
| with gr.Tab("Net"): | |
| net_btn = gr.Button("Get Net Info") | |
| net_out = gr.Textbox(label="net") | |
| net_btn.click(net_info, None, net_out) | |
| with gr.Tab("File"): | |
| file_in = gr.Textbox(label="path") | |
| file_out = gr.Textbox(label="content") | |
| file_in.submit(read_file, file_in, file_out) | |
| demo.launch() | |