Python调用飞信接口发送短信

# -*- coding:utf-8 -*-  
# file: SendFetion.py  
# by Lee 2013-9-18  
"""----------------------------------------------------------------------------- 
    使用HTTPS调用飞信接口: 
    https://quanapi.sinaapp.com/fetion.php?u=飞信登录手机号&p=飞信登录密码&to=接收飞信的手机号&m=飞信内容 
    返回结果为Json格式,result=0时表示发送成功 
    {“result”:0,”message”:”\u53d1\u9001\u6210\u529f”} 
-----------------------------------------------------------------------------"""  
import sys  
import httplib  
import urllib  
import re  
import time 
import json  
   
class Fetion:  
    """ 
       model to call fetionapi. 
       attribute:url, fromTel, pwd, toTel, msg 
       function:Trans, format_url, SendMsg 
    """  
    url = "https://quanapi.sinaapp.com/fetion.php?u=" 
    def __init__(self,  
                 toTel,  
                 msg,  
                 fromTel = '151xxxxxxxx',  # 这里是发送方号码 和 密码  
                 pwd = 'pxxxxxxxx'):  
        self.fromTel = fromTel  
        self.pwd = pwd  
        self.toTel = toTel  
        self.msg = self.Trans(msg)  
    def Trans(self, msg):  
        # change space to '%20', otherwise error raised  
        return re.sub(" ", "%20", str(msg))  
    def format_url(self):  
        url_address = self.url + self.fromTel \  
            + "&p=" + self.pwd \  
            + "&to=" + self.toTel \  
            + "&m=" + self.msg  
        return url_address  
    def SendMsg(self):  
        # call the api by http get method  
        return urllib.urlopen(self.format_url())  
def msg2log(msg):  
    logfile = open('MyFetion.log', 'a')  
    now = time.strftime('%Y%m%d %H:%M:%S')  
    logfile.write('\n'+ now + '\n' + msg + '\n')  
    logfile.close()  
    
def main():  
    # format mutual message  
    print "\n" + " "*10 + "*"*60  
    print " "*10 + " Personal Fetion" 
    print " "*10 + " Enter the number and message what you want to send to." 
    print " "*10 + " blank number means yourself," 
    print " "*10 + " and a blank message line to exit." 
    print " "*10 + "*"*60  
    # get the destination phone number  
    toTel = raw_input("Input the target telphone number:")  #控制台输入手机号也可以改成固定的直接赋值就可,这里填的是接收方号码。
    if toTel == "":  
        toTel = "151xxxxxxxx"  # 如果控制台没有输入手机号就默认使用这个toTel的值 
    # get the message and send by Fetion class  
    while True:  
        msg = raw_input("Message:")     #输入要发送的短信消息
        if msg == "":  
            break  # 为空就退出 
        else:  
            print "Sending...." 
            msg2log(msg)  
            ff = Fetion(toTel, msg)  
            answer = ff.SendMsg()  
            data = answer.read()  
            jdata=json.loads(data)  
            if jdata['result']==0:  #判别信息发送成功或失败
                print 'Done.^_^\n' 
            else:  
                print 'Fail.-_=\n'  
           
if __name__ == '__main__':  
    main()  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值