工作原因,要定时检测服务状态,最开始想用微信公众号,发现只有企业认证帐号才可以给用户发送信息,这个方案给否掉了。
有一天看书突然发现在TWILIO免费发短信功能,试用后非常方便。
一、申请twilio帐号
地址:https://www.twilio.com/
注意:邮箱要用163.com gmail.com都可以,用QQ邮箱不行,通过不了,
申请完帐号后在申请一个临时的电话号码,这个号码是用来给自己发送短信的。
二、写python 接口
sms.py
点击(此处)折叠或打开
- #coding: utf-8
- from twilio.rest import Client
- def sms(t):
- account_sid = " "
-
- auth_token = " "
-
- client = Client(account_sid, auth_token)
-
- message = client.messages.create(
- to="= ",#自己的手要号码
- from_=" ",#在网站上申请的手机码,只能用这个号码发信息
- body=t.decode("utf-8"))#编码密码使用utf-8才发送中文。
-
- print(message.sid)
引用sms模块
点击(此处)折叠或打开
- #!/usr/bin/python
- import socket
- import time
- from sms import sms
- import logging
- import time
- def port_try(host,port):
- shijian=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
- sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sk.settimeout(1)
- try:
- sk.connect((host,port))
- t=host+"服务器 "+str(port)+" 连接正常 "+str(shijian)
- #sms(t)
- print t
- except Exception:
- w=host+" 服务器 "+str(port)+" 无法连接 "+str(shijian)
- print w
- sms(w)
- sk.close()
-
- file = open("/root/script/ip.txt")#ip地址写法192.16.0.1:80
-
- while True:
- line = file.readline().strip('\n')
- if len(line)==0:break
- ip=str(line.split(':',1)[0])
- port=int(line.split(':',1)[1])
- port_try(ip,port)
- #time.sleep(2)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11363642/viewspace-2139238/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/11363642/viewspace-2139238/