Python/Python for Linux
[Python] Linux에서 간단한 Web 모니터링 하기
Pydole
2019. 4. 15. 19:31
Python Version : 2.7.5
#!/usr/bin/python
import requests as req
from datetime import datetime
import time
sites = ('http://192.168.x.77','http://192.168.x.77/xxxx.php')
for site in sites:
try:
r = req.get(site)
print site, 'return_code :', r.status_code
except Exception as e:
print site, 'fail'
time.sleep(10)
★ 웹 페이지가 기동 중 일 때, 아래와 같이 응답코드 값을 리턴 받을 수 있습니다.
http://192.168.x.77 return_code : 200 (200은 정상)
http://192.168.x.77/xxxx.php return_code : 404 (404은 페이지 없음)
if문을 활용해서 응답코드 값이 200이면 정상처리 하면 좋겠습니다.
★ 웹 페이지 비정상 동작일 때는 예외처리를 걸었기 때문에 fail 메세지가 발생합니다.
http://192.168.x.77 fail
http://192.168.x.77/xxxx.php fail
★ Access Log Agent 필드에 Python으로 로깅 되네요.
"GET / HTTP/1.1" 200 3512 "-" "python-requests/2.21.0"