Web服务端口可用性检测工具

用于检测某个http端口是否正常:

import httplib
import datetime
import time
import os

output_file = open('/root/as.link.fiatauto.com.out','w')

while True:
    is_ok = True
    status = ''
    expt = ''
    try:
        conn = httplib.HTTPSConnection('as.link.fiatauto.com',443,timeout=10)  
        conn.request('GET', '/', '', {'user-agent':'test'})  
        r = conn.getresponse()
        if r.status != 200 and r.status != 302:
            is_ok = False
        status = r.status
    except Exception,e:
        is_ok = False
        expt = e
    if is_ok:
        output_file.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " Good, Status is: " + str(status))
    else:
        output_file.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " Bad, " + "Status is: " + str(status) + ", Exception is: " + str(expt))
    output_file.write('\n')
    output_file.flush()
    time.sleep(5)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*