seawolf2357 commited on
Commit
4a056c9
โ€ข
1 Parent(s): be19db7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -23
app.py CHANGED
@@ -1,35 +1,29 @@
1
  import gradio as gr
2
- import aiohttp
3
- import asyncio
4
  import threading
 
5
 
6
- # ๋น„๋™๊ธฐ์ ์œผ๋กœ ์™ธ๋ถ€ URL์— ์ ‘์†ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
7
- async def check_connection(url):
8
  try:
9
- async with aiohttp.ClientSession() as session:
10
- async with session.get(url) as response:
11
- status = f"URL: {url} ์ƒํƒœ ์ฝ”๋“œ: {response.status}, ์ ‘์† ์ƒํƒœ: {'์ ‘์† ์„ฑ๊ณต' if response.status == 200 else '์ ‘์† ์‹คํŒจ'}"
12
- print(status)
13
- return status
14
  except Exception as e:
15
  status = f"URL: {url} ์ ‘์† ์‹คํŒจ: {str(e)}"
16
- print(status)
17
- return status
18
 
19
- # ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ•ด์ง„ ์ฃผ๊ธฐ๋กœ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
20
- async def start_timer(urls, interval):
21
- while True:
22
- for url in urls:
23
- if url: # URL์ด ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์ฒดํฌ
24
- await check_connection(url)
25
- await asyncio.sleep(1) # ๊ฐ URL ์ฒดํฌ ํ›„ 1์ดˆ ๋Œ€๊ธฐ
26
- await asyncio.sleep(interval - len(urls)) # ๋ชจ๋“  URL ์ฒดํฌ ํ›„ interval์—์„œ ์ด๋ฏธ ์†Œ๋น„๋œ ์‹œ๊ฐ„์„ ๋บ€ ๋งŒํผ ๋Œ€๊ธฐ
27
 
28
  # ํƒ€์ด๋จธ ์‹œ์ž‘ ํ•จ์ˆ˜๋ฅผ Gradio์˜ ์ž…๋ ฅ๊ณผ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
29
  def setup_timer(interval, *urls):
30
  interval_seconds = interval * 60 # ๋ถ„์„ ์ดˆ๋กœ ๋ณ€ํ™˜
31
- # ๋น„๋™๊ธฐ ์ž‘์—…์„ ๋ณ„๋„์˜ ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰
32
- threading.Thread(target=asyncio.run, args=(start_timer(urls, interval_seconds),)).start()
 
33
  return "ํƒ€์ด๋จธ๊ฐ€ ์„ค์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
34
 
35
  # Gradio UI ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
@@ -89,6 +83,5 @@ app = gr.Interface(
89
  ],
90
  cache_examples=False # ์บ์‹œ ๋น„ํ™œ์„ฑํ™”
91
  )
 
92
 
93
- if __name__ == "__main__":
94
- app.launch()
 
1
  import gradio as gr
2
+ import requests
 
3
  import threading
4
+ import time
5
 
6
+ # ์™ธ๋ถ€ URL์— ์ ‘์†ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
7
+ def check_connection(url):
8
  try:
9
+ response = requests.get(url)
10
+ status = f"URL: {url} ์ƒํƒœ ์ฝ”๋“œ: {response.status_code}, ์ ‘์† ์ƒํƒœ: {'์ ‘์† ์„ฑ๊ณต' if response.status_code == 200 else '์ ‘์† ์‹คํŒจ'}"
 
 
 
11
  except Exception as e:
12
  status = f"URL: {url} ์ ‘์† ์‹คํŒจ: {str(e)}"
13
+ print(status)
14
+ return status
15
 
16
+ # ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ•ด์ง„ ์ฃผ๊ธฐ๋กœ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
17
+ def start_timer(url, interval):
18
+ threading.Timer(interval, start_timer, [url, interval]).start()
19
+ check_connection(url)
 
 
 
 
20
 
21
  # ํƒ€์ด๋จธ ์‹œ์ž‘ ํ•จ์ˆ˜๋ฅผ Gradio์˜ ์ž…๋ ฅ๊ณผ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
22
  def setup_timer(interval, *urls):
23
  interval_seconds = interval * 60 # ๋ถ„์„ ์ดˆ๋กœ ๋ณ€ํ™˜
24
+ for url in urls:
25
+ if url: # URL์ด ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ํƒ€์ด๋จธ ์‹œ์ž‘
26
+ start_timer(url, interval_seconds)
27
  return "ํƒ€์ด๋จธ๊ฐ€ ์„ค์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
28
 
29
  # Gradio UI ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
 
83
  ],
84
  cache_examples=False # ์บ์‹œ ๋น„ํ™œ์„ฑํ™”
85
  )
86
+ app.launch()
87