-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_url_alive_chk_using_crawler.py
138 lines (122 loc) · 4.29 KB
/
json_url_alive_chk_using_crawler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import requests, sys, time, daemon, os, socket, string, sys
import json, urllib.request, urllib.parse
from requests.exceptions import Timeout
from urllib.parse import urljoin
from urllib.error import URLError, HTTPError, ContentTooShortError
"""
서버 라벨 설정(서버 또는 호스트의 이름)
"""
serverName = 'TEST 서버'
title = '[' + serverName + ']\n'
"""
Telegram bot의 token value 설정
"""
my_token = 'TOKEN'
my_id = 'CHAT_ID'
"""
봇에 메시지를 전달하는 설정
"""
bot = telegram.Bot(token = my_token)
"""
메시지 전송 설정
"""
def send(chat):
bot.sendMessage(my_id, chat, parse_mode='HTML')
"""
감시 대상 URL:port
"""
url_1 = "http://www.example.com/GetLatestVersion.json"
url_2 = "http://upload.example.com/GetLatestVersion.json"
"""
json 구조를 dictionary 자료형으로 변환하는 함수
"""
#def json_to_dictionary(url):
# try:
# text_data = urllib.request.urlopen(url).read().decode('utf-8')
# dict_ = json.loads(text_data)
# except (URLError, HTTPError, ContentTooShortError) as e:
# dict_ = False
# return dict_
def json_to_dictionary(url, num_retries=3): #예외 발생시 3번 재시도
try:
text_data = urllib.request.urlopen(url).read().decode('utf-8')
dict_ = json.loads(text_data)
except (URLError, HTTPError, ContentTooShortError) as e:
dict_ = None
if num_retries > 0: #예외 발생시 3번 재시도
return json_to_dictionary(url, num_retries - 1)
return dict_
"""
변환된 dictionary 자료형에서 특정 자료형에 해당하는 값이 'OK'인지 검증하는 함수
여기서 'result', 'code', 'OK'는 예제이다. 실제 json 코드는 개발한 페이지마다 다르다.
json 페이지를 파싱하면 {"result":{"msg":"최신버전입니다.","code":"OK"}} 이런 결과 값이 나온다는 가정을 전제한 것.
"""
def url_status_chk(url):
try:
result = json_to_dictionary(url)
if result['result']['code'] == 'OK':
return True
except:
return False
"""
현재 URL 서비스 상태 저장
"""
current_url_1_status = url_status_chk(url_1)
current_url_2_status = url_status_chk(url_2)
"""
데몬 생성 함수
"""
def daemon():
try:
pid = os.fork()
if pid > 0:
print('PID: %d' % pid)
sys.exit()
except OSError as error:
print('Unable to fork. Error: %d (%s)' % (error.errno, error.strerror))
sys.exit()
GET_URL_STATUS_CHK()
def GET_URL_STATUS_CHK():
os.setsid()
os.open("/dev/null", os.O_RDWR)
os.dup(0)
os.dup(0)
status_of_url1 = True # URL 상태 기본 값 = True
status_of_url2 = True
while True:
try:
msg = title
current_url_1_status = url_status_chk(url_1)
print(current_url_1_status)
if current_url_1_status == True:
if status_of_url1 == True:
status_of_url1 = False
msg += url_1 + " 텔레그램 봇이 전달할 텍스트 메시지.\r\n"
send(msg)
elif current_url_1_status == False:
if status_of_url1 == False:
status_of_url1 = True
msg += url_1 + " [치명]텔레그램 봇이 전달할 텍스트 메시지.\r\n"
send(msg)
except:
pass
try:
msg = title
current_url_2_status = url_status_chk(url_2)
if current_url_2_status == True:
if status_of_url2 == True:
status_of_url2 = False
msg += url_2 + " 텔레그램 봇이 전달할 텍스트 메시지.\r\n"
send(msg)
elif current_url_2_status == False:
if status_of_url2 == False:
status_of_url2 = True
msg += url_2 + " [치명]텔레그램 봇이 전달할 텍스트 메시지.\r\n"
send(msg)
except:
pass
time.sleep(3)
if __name__ == '__main__':
daemon()