Python-向飞信好友发送短信,可用于日常监控程序
zhangbo2012 发布于 2013年09月06日 10时,
8评/1383阅
参照oschina上某位大牛写的php代码,改成python版本,实现向飞信好友下发短信功能。
该功能可用于日常监控脚本中,当发现系统异常时主动向自己的手机上发送短信,避免邮件告警处理不及时的问题。
该功能可用于日常监控脚本中,当发现系统异常时主动向自己的手机上发送短信,避免邮件告警处理不及时的问题。
标签: <无>
代码片段(1)[全屏查看所有代码]
1. [代码][Python]代码
01 | #coding:utf-8 |
02 | #调用飞信 |
03 |
04 | import urllib2 |
05 | import urllib |
06 |
07 |
08 | class fetion: |
09 | msisdn = '' |
10 | passwd = '' |
11 | baseurl = 'http://quanapi.sinaapp.com/fetion.php' |
12 |
13 | #设置登陆用户 |
14 | def setuser( self ,msisdn,passwd): |
15 | self .msisdn = msisdn |
16 | self .passwd = passwd |
17 |
18 | #发送短信 |
19 | def sendsms( self ,recmsisdn,content): |
20 | cod = 1 |
21 | if self .msisdn = = '' : |
22 | cod = 0 |
23 | return 'msisdn is null' |
24 |
25 | if self .passwd = = '' : |
26 | cod = 0 |
27 | return 'passwd is null' |
28 |
29 | if recmsisdn = = '' : |
30 | cod = 0 |
31 | return 'recmsisdn is null' |
32 |
33 | if content = = '': |
34 | cod = 0 |
35 | return 'content is null' |
36 |
37 | if cod = = 1 : |
38 | smsurl = self .baseurl + '?u=' + self .msisdn + '&p=' + self .passwd + '&to=' + recmsisdn + '&m=' + content |
39 | print smsurl |
40 |
41 | res = urllib2.urlopen(smsurl).read() |
42 | #反馈调用结果 |
43 | return res.split( ',' )[ 0 ].split( ':' )[ 1 ] |
44 |
45 |
46 | newfetion = fetion() |
47 |
48 | #设置登陆飞信的用户 |
49 | newfetion.setuser( '13812341234' , '123456' ) |
50 | #发送短信 |
51 | print newfetion.sendsms( '13812341235' , '测试' ) |