import logging import aiohttp import asyncio import gradio as gr from datetime import datetime import pytz logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) timezone = pytz.timezone('Asia/Shanghai') urls = [ 'https://asfag654-sss.hf.space', 'https://asfag654-g.hf.space', 'https://asfag654-kk.hf.space', 'https://asfag654-t.hf.space', 'https://shegy-k.hf.space', 'https://shegy-kg.hf.space', 'https://sadg456-bo.hf.space','https://sadg456-sleepy.hf.space','https://sadg456-g.hf.space' ] status = {url: {'status_code': None, 'message': ''} for url in urls} lock = asyncio.Lock() async def probe(): async with aiohttp.ClientSession() as session: while True: tasks = [check_url(session, url) for url in urls] await asyncio.gather(*tasks) await asyncio.sleep(13600) async def check_url(session, url): try: async with session.get(url) as response: async with lock: status[url]['status_code'], current_time = response.status, datetime.now(timezone).strftime('%m-%d %H') status[url]['message'] = f"Service is {'up and running' if response.status == 200 else 'down or returned an error'} ({current_time})" logger.info(f"{current_time} - {url} status: {response.status}") except Exception as e: async with lock: status[url]['message'] = f'An error occurred: {e} ({datetime.now(timezone).strftime("%m-%d %H:%M")})' logger.error(f"{datetime.now(timezone).strftime('%m-%d %H:%M')} - Error checking {url}: {e}") def get_status(): return {url: status[url]['message'] for url in urls} def launch_gradio(): with gr.Blocks() as demo: gr.Markdown("# URL 状态监控") status_output = gr.JSON(label="状态信息") refresh_button = gr.Button("刷新状态") refresh_button.click(lambda: get_status(), outputs=[status_output]) demo.launch() if __name__ == "__main__": loop = asyncio.get_event_loop() loop.create_task(probe()) launch_gradio()