python绠$悊windows绯荤粺tomcat鏈嶅姟

鍩轰簬python瀹炵幇鐨勫姛鑳芥洿寮哄ぇ鐨則omcat 瀹堟姢杩涚▼銆傜洰鍓嶅疄鐜扮殑鍔熻兘锛�

1銆侀�氳繃鎬ф洿寮� 锛嶏紞锛嶉�氳繃淇敼config鏂囦欢鍗冲彲瀹炵幇瀵瑰叾浠杢omcat绋嬪簭鐨勫畧鎶わ紱

2銆佸叿鏈夋棩蹇楁煡鐪� 锛嶏紞锛� 杞30涓棩蹇楁枃浠讹紱

3銆佹棤闇�鍊熷姪curl 绛夊伐鍏锋垨瀹夎鍏朵粬鐜 锛嶏紞锛嶅�熷姪py2exe鍙互灏唒ython鑴氭湰鍙互鐢熸垚exe鏂囦欢锛�

浠g爜濡備笅锛�

#!-*- encoding: utf-8 -*-

# code by yangbk

# mysite: www.361way.com

import urllib2

import logging

import os

import time

from ConfigParser import ConfigParser

from logging.handlers import TimedRotatingFileHandler

LOG_FILE = "./logs/output.log"

logger = logging.getLogger()

logger.setLevel(logging.INFO)

fh = TimedRotatingFileHandler(LOG_FILE,when='midnight',interval=1,backupCount=7)聽聽 //姣忓ぉ

datefmt = '%Y-%m-%d %H:%M:%S'

format_str = '%(asctime)s %(levelname)s %(message)s '

formatter = logging.Formatter(format_str, datefmt)

fh.setFormatter(formatter)

fh.suffix = "%Y%m%d%H%M"

logger.addHandler(fh)

def getUrlcode(url):

try:

start = time.time()

response = urllib2.urlopen(url,timeout=10)

msg = 'httpcode is : ' + str(response.getcode()) + ' - open use time :' + str((time.time()-start)*1000) + 'ms'

logging.info(msg)

return response.getcode()

except urllib2.URLError as e:

msg = 'open url error ,reason is:' + str(e.reason)

logging.info(msg)

def get(field, key):

result = ""

try:

result = cf.get(field, key)

except:

result = ""

return result

def read_config(config_file_path, field, key):

cf = ConfigParser()

try:

cf.read(config_file_path)

result = cf.get(field, key)

except:

sys.exit(1)

return result

CONFIGFILE='./cfg/config.ini'

os.environ["JAVA_HOME"] = read_config(CONFIGFILE,'MonitorProgram','JAVA_HOME')

os.environ["CATALINA_HOME"] = read_config(CONFIGFILE,'MonitorProgram','CATALINA_HOME')

ProgramPath = read_config(CONFIGFILE,'MonitorProgram','StartPath')

ProcessName = read_config(CONFIGFILE,'MonitorProcessName','ProcessName')

url = read_config(CONFIGFILE,'MonitorUrl','Url')

#url = "http://dh.361way.com/"

while True:

HttpCode = getUrlcode(url)

if HttpCode is not 200:

command = 'taskkill /F /FI "WINDOWSTITLE eq ' + ProcessName + '"'

os.system(command)

os.system(ProgramPath)

time.sleep(30)

config.ini鏂囦欢鐨勫唴瀹瑰涓嬶細

[MonitorProgram]

StartPath: C:/tomcat/bin/startup.bat

CATALINA_HOME: C:\\tomcat\\

JAVA_HOME: C:\\Program Files\\Java\\jdk1.8.0_31

[MonitorProcessName]

ProcessName: tomcat_8080

[MonitorUrl]

Url: http://127.0.0.1:8080

浠ヤ笂浠g爜濡傛灉浠呬粎鍙拡瀵瑰崟涓簲鐢紝浠g爜鍙互鏇寸畝娲侊紝鍥犱负閰嶇疆鏂囦欢閮ㄥ垎鍙互涓嶉�氳繃configparse妯″潡杩涜璇诲彇 銆傝�屼笖杩欓噷瀵筩onfigparse妯″潡杩涜浜嗛噸鏂板鐞嗭紙get鍑芥暟涓巖ead_config鍑芥暟锛夛紝閬垮厤鑾峰彇鐨刱ey涓嶅瓨鍦ㄦ椂锛屽嚭鐜扮殑鎶ラ敊鎯呭喌銆傚叿浣撳嚭閿欐椂鐨勬姤閿欒涓嬪浘锛�

configparser

浜х敓鐨勬棩蹇楁枃浠跺唴瀹瑰涓嬶細

2016-10-09 15:08:30 INFO open url error ,reason is:[Errno 10061]

2016-10-09 15:09:00 INFO httpcode is 200 - open url use time 111.999988556ms

2016-10-09 15:09:30 INFO httpcode is 200 - open url use time 7.99989700317ms

2016-10-09 15:10:00 INFO httpcode is 200 - open url use time 6.00004096167ms

闄ゆ涔嬪锛岃繕鍙互澧炲姞閭欢閫氱煡鍔熻兘锛屽叿浣撳彲浠ュ弬鑰冿細python閭欢鍙戦�佹ā鍧�

浠g爜绋嶄綔淇敼锛屼篃鍙互寰堟柟渚跨殑閫氳繃绔彛妫�娴嬬‘璁や笟鍔℃槸鍚﹀仴搴凤紝绔彛妫�娴嬬殑鑴氭湰鍐呭濡備笅锛�

import os

import socket

def IsOpen(ip,port):

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

try:

s.connect((ip,int(port)))

s.shutdown(2)

print '%d is open' % port

return True

except:

print '%d is down' % port

return False

if __name__ == '__main__':

IsOpen('127.0.0.1',800)

你可能感兴趣的:(python绠$悊windows绯荤粺tomcat鏈嶅姟)