#!/usr/bin/python # coding: UTF-8 # # Webサーバ 生死監視ツール # # ping風にサーバを見たかったので作ってみた # # 起動例 # $ python3 webping.py import http.client,datetime,os import time result = "" #結果の書式 def format_result(address,response): return address +" "+ str(response.status) + " " + response.reason #サーバをチェックする def checkserver(address): global result time.sleep(1) conn = http.client.HTTPConnection( address, timeout = 10 ) try: conn.request( "GET", "/" ) except: result = str(datetime.datetime.now()) + " " + result + address + " not found." print(result) result = ""; return response = conn.getresponse() result = now = str(datetime.datetime.now()) + " " + result + format_result(address,response) print(result) result = ""; conn.close() # ここでサーバをチェックする while 1: # checkserver("kobore.net") checkserver("localhost")