码迷,mamicode.com
首页 > 编程语言 > 详细

检测端口状态的python脚本

时间:2016-11-14 12:07:33      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:phone   exce   接口   std   style   com   encode   code   ica   

#!/usr/bin/env python
import os,subprocess,socket,time,sys
from urllib import urlencode
from socket import gethostname

def check_port():
        port = [8091,8080]
        failed_port = []
        for _each_port in port:
                #print i
                try:
                        sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        sk.settimeout(2)
                        sk.connect((localhost,_each_port))
                        sk.close
                        now = time.strftime("%Y-%m-%d %H:%M:%S")
                        tt = "check port success!"
                        logfile = "/home/op/check.log"
                        f = open(logfile,a+)
                        f.write(now + " " + tt + "\n")
                        f.close()
                except socket.error:
                        failed_port.append(_each_port)

        for _each_port in failed_port:
                name = gethostname()
                message = "host: %s port: %d down" %(name, _each_port)
                tel = [电话号码]
                for _each_phone in tel:
                        data = {
                                "phone": _each_phone,
                                "message": message
                        }
                        command=curl "http://"短信接口ip":8080/message.php?%s" %(urlencode(data))
                        print command
                        ck = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
                        std_ok, std_err = ck.communicate(input=None)
                        #print std_ok
                        #print std_err

if __name__ == __main__:
        check_port()

 

检测端口状态的python脚本

标签:phone   exce   接口   std   style   com   encode   code   ica   

原文地址:http://www.cnblogs.com/uglyliu/p/6061023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!