Spaces:
Runtime error
Runtime error
from http.server import HTTPServer, BaseHTTPRequestHandler | |
import datetime | |
import json | |
import threading | |
import time | |
import requests | |
import logging | |
from urllib3.exceptions import InsecureRequestWarning | |
# SSL κ²½κ³ λΉνμ±ν | |
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
# λ‘κΉ μ€μ | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s' | |
) | |
URLS = [ | |
"https://huggingface.co/spaces/ginipick/discord-openfree-LLM-chatgpt4", | |
"https://huggingface.co/spaces/fantos/discord-openfree-LLM-qwen3-30b-a3b", | |
"https://huggingface.co/spaces/fantos/discord-openfree-LLM-qwen3-235b-a22b", | |
"https://huggingface.co/spaces/fantos/discord-openfree-LLM-llama4-maverick-instruct", | |
"https://huggingface.co/spaces/fantos/discord-openfree-LLM-llama4-scout-instruct", | |
"https://huggingface.co/spaces/fantos/discord-openfree-LLM-deepseek-v3-0324", | |
"https://huggingface.co/spaces/fantos/discord-openfree-Image-sdxl-lightning", | |
"https://huggingface.co/spaces/fantos/discord-openfree-Image-flux", | |
"https://huggingface.co/spaces/fantos/discord-openfree-Image-3d", | |
"https://huggingface.co/spaces/fantos/discord-openfree-video-luma", | |
"https://huggingface.co/spaces/fantos/discord-openfree-video-luma2" | |
] | |
# ν κ°κ²© (μ΄) - νκΉ νμ΄μ€ μ¬λ¦½ νμμμλ³΄λ€ μ§§μμΌ ν¨ | |
PING_INTERVAL = 300 # 5λΆλ§λ€ ν | |
class HealthCheckHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
# κΈ°λ³Έ ν€λ μ€μ | |
self.send_response(200) | |
self.send_header('Content-type', 'application/json') | |
self.send_header('Access-Control-Allow-Origin', '*') | |
self.end_headers() | |
# μν μ 보 μ€λΉ | |
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
status_info = { | |
"status": "ok", | |
"timestamp": current_time, | |
"message": "Service is running" | |
} | |
# JSON μλ΅ λ°ν | |
self.wfile.write(json.dumps(status_info).encode()) | |
def check_url(url): | |
"""URL μνλ₯Ό νμΈνκ³ κ²°κ³Όλ₯Ό λ°νν©λλ€.""" | |
try: | |
# HEAD μμ²μ΄ GETλ³΄λ€ λΉ λ¦ (λ³Έλ¬Έ μμ΄ ν€λλ§ λ°ν) | |
response = requests.head( | |
url, | |
verify=False, # SSL κ²μ¦ λΉνμ±ν | |
timeout=10, # νμμμ μ€μ | |
allow_redirects=True # 리λ€μ΄λ νΈ νμ© | |
) | |
status_code = response.status_code | |
# 200 OKμΈ κ²½μ° μ±κ³΅ | |
if status_code == 200: | |
result = f"μν μ½λ: {status_code}, μ μ μν: μ μ μ±κ³΅" | |
else: | |
result = f"μν μ½λ: {status_code}, μ μ μν: μ μ μ€ν¨" | |
except Exception as e: | |
result = f"μ μ μ€ν¨: {str(e)}" | |
logging.info(f"URL: {url} {result}") | |
return result | |
def ping_thread(): | |
""" | |
μ£ΌκΈ°μ μΌλ‘ URLλ€μ νμΈνμ¬ μ¬λ¦½ λͺ¨λλ‘ μ νλμ§ μλλ‘ ν©λλ€. | |
""" | |
logging.info("μλ ν μ€λ λ μμ") | |
while True: | |
for url in URLS: | |
check_url(url) | |
# λ€μ νκΉμ§ λκΈ° | |
time.sleep(PING_INTERVAL) | |
def run_server(port=7860): | |
""" | |
ν¬μ€ μ²΄ν¬ μλ²λ₯Ό μ€ννκ³ λμμ λ°±κ·ΈλΌμ΄λ ν μ€λ λλ₯Ό μμν©λλ€. | |
""" | |
# ν μ€λ λ μμ | |
ping_task = threading.Thread(target=ping_thread, daemon=True) | |
ping_task.start() | |
# μΉ μλ² μ€μ | |
server_address = ('0.0.0.0', port) | |
httpd = HTTPServer(server_address, HealthCheckHandler) | |
logging.info(f"Starting health check server on port {port}") | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
run_server() |