Bjo53 commited on
Commit
60c3505
·
verified ·
1 Parent(s): dc23d6c

Upload keepalive.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. keepalive.py +17 -32
keepalive.py CHANGED
@@ -1,42 +1,27 @@
1
- import os
2
  import time
3
  import random
4
- import threading
5
  import requests
 
6
 
7
- def human_ping():
8
  while True:
9
  try:
10
- host = os.environ.get('SPACE_HOST', '')
11
- if host:
12
- headers = {
13
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
14
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
15
- 'Accept-Language': 'en-US,en;q=0.5',
16
- 'Accept-Encoding': 'gzip, deflate, br',
17
- 'Connection': 'keep-alive',
18
- 'Upgrade-Insecure-Requests': '1',
19
- }
20
- resp = requests.get(f'https://{host}/', headers=headers, timeout=15)
21
- print(f'[HumanPing] {time.strftime("%H:%M:%S")} - Status: {resp.status_code}')
22
- except Exception as e:
23
- print(f'[HumanPing] Error: {e}')
24
 
25
- # Truly random interval: 15-90 minutes (no pattern)
26
- wait = random.randint(900, 5400)
27
- print(f'[HumanPing] Next ping in {wait//60}m {wait%60}s')
28
  time.sleep(wait)
29
 
30
- def activity_check():
31
- while True:
32
- try:
33
- os.system('date > /tmp/last_activity')
34
- os.system('whoami > /dev/null')
35
- except:
36
- pass
37
- time.sleep(random.randint(300, 600))
38
-
39
- threading.Thread(target=human_ping, daemon=True).start()
40
- threading.Thread(target=activity_check, daemon=True).start()
41
 
42
- print('[KeepAlive] Started human-like ping service')
 
 
 
 
1
  import time
2
  import random
 
3
  import requests
4
+ import threading
5
 
6
+ def ping(url, name):
7
  while True:
8
  try:
9
+ headers = {
10
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
11
+ 'Accept': 'text/html',
12
+ }
13
+ resp = requests.get(url, headers=headers, timeout=15)
14
+ print(f'[{name}] {time.strftime("%H:%M:%S")} -> {resp.status_code}', flush=True)
15
+ except:
16
+ print(f'[{name}] Error', flush=True)
 
 
 
 
 
 
17
 
18
+ wait = random.randint(900, 3600)
19
+ print(f'[{name}] Next {wait//60}m', flush=True)
 
20
  time.sleep(wait)
21
 
22
+ # Ping keep-alive space back
23
+ threading.Thread(target=ping, args=('https://bjo53-keep-alive.hf.space/', 'KeepAlive'), daemon=True).start()
 
 
 
 
 
 
 
 
 
24
 
25
+ print('[Mutual] Started', flush=True)
26
+ while True:
27
+ time.sleep(60)