使用python移动飞信模块发送短信

来源:互联网 发布:百度人工智能研究院 编辑:程序博客网 时间:2024/05/11 15:23

 作者:miaoo

1.应用场景

由于自己做的一个系统需要用到发送短信到自己手机的功能,于是搜索了一下,发现了一个通过移动飞信通道发送短信开源库:PyFetion

PyFetion 模拟实现了飞信的通信协议,所以能够实现的功能非常多:短信收发、好友管理、修改状态等等等。

但是,由于我只需要发送短信,所以其它功能都很多余;再加上使用PyFetion 登录飞信时可能需要输入验证码,所以不太适合自动化系统的调用。

继续搜索发现了飞信为手机用户提供了一个wap站点:http://f.10086.cn

PS:由于是这一个wap站点,您可能需要在FireFox中安装扩展(Extension):wmlbrowser ,以便正常的浏览.

通过它能够进行在线信息收发。由于wap站点代码结构比较简单,所以很适合用程序模拟用户登录、发送信息的整个流程,以达到发送短信的目的。

2.代码分析

代码主要用到了下面几个lib

cj = cookielib.LWPCookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))urllib2.install_opener(opener)

登陆时,首先要处理Cookie信息

cj = cookielib.LWPCookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))urllib2.install_opener(opener)

其次,我们在登录界面填写手机号及密码后,点击登录按钮,浏览器会通过POST方法向服务器提交登录信息以便验证身份。用Firefox的Httpfox插件可以抓包查看浏览器提交的数据内容:

可以看到,在点击登录后浏览器发送POST 请求提交登录数据,其中:pass 为密码,loginstatus为登录状态(4表示隐身),m为手机号码。我们在python中定义一个字典类型变量记录要模拟提交的数据:

 parameter = {  'pass':‘你的密码’,  'm':'你的手机号',  'loginstatus':4}

然后,生成POST请求,并发送:

url_login = 'http://f.10086.cn/im/login/inputpasssubmit1.action'req = urllib2.Request(           #生存POST请求  url =url_login ,  urllib.urlencode(parameter))jump =  urllib2.urlopen(req)     #发送请求

在提交登录请求后,服务器回返回一个跳转页面,其中包含一个跳转连接(URL),如果登录成功,则返回:

/im/index/indexcenter.action?t=xxxxxxxxxxxxxxxxx

其中xxx代表一串数字。如果登录失败,则返回先前的登录页:

/im/login/login.action

我们用正则表达式在页面中提取出这个链接,判断登录是否成功(关于正则表达式的内容,推荐:正则表达式30分钟入门教程):

page = jump.read();#获取跳转链接url = re.compile(r'id="start".*?ontimer="(.*?);').findall(page)[0]if url == '/im/login/login.action':        print 'Login Failed!'       #登录失败        raw_input('Press any key to exit.')        returnelse:        print 'Login Successfully!' #登录成功

同时,我们也将连接尾部那一串数字参数提取出来,以备待会儿使用:

arg_t = re.compile(r't=(d*)').findall(page)[0]    #获取参数

同样的方法,我们可以抓包提取到发送短信时POST的数据内容,并用程序模拟提交:

url_sendmsg = 'http://f.10086.cn/im/user/sendMsgToMyselfs.action'sendmsg = urllib2.Request(  url =url_sendmsg,  urllib.urlencode('msg':‘你要发送的消息’.decode('gbk').encode('utf-8')))res = urllib2.urlopen(sendmsg)

通过提交POST请求后返回的连接判断发送是否成功:

if res.geturl == 'http://f.10086.cn/im/user/sendMsgToMyself.action' :  print 'Send Failed!'else:  print 'Send Successfully!'

最后注销退出:

logout = urllib2.Request(url_logout + arg_t)response = urllib2.urlopen(logout)print 'Logout Successfully!'

 

完整的代码可以看下面。整个代码共45行,在Python 2.7下编译通过。

3.改进

目前只实现了发送短信到自己手机的功能(当然,这就是我目前所需要的),其实,我们在完成登录操作后,便能够提取出好友列表,用上文类似的方法就能够给任意的飞信好友发送短信了。这个功能留到以后需要的时候再完成吧。

4.总结

本文主要使用了:

urllib2.Request(xxx)urllib2.urlopen(xxx)

可以看到,在python中使用urllib2可以很方便的进行各种网页相关的交互操作,如页面抓取、表单提交等等,再配合正则表达式,可以构造出各种有趣的应用。

完整代码:

 

# -*- coding: utf-8 -*- import cookielibimport urllibimport urllib2import reurl_login = 'http://f.10086.cn/im/login/inputpasssubmit1.action'url_logout = 'http://f.10086.cn//im/index/logoutsubmit.action?t='url_msg = 'http://f.10086.cn/im/user/sendMsgToMyselfs.action'user = 'Your Phone Number'password = 'Your Passwrdd'loginstatus = '4' #��¼״̬,4��ʾ����arg_t = ''def fetion(msg):    cj = cookielib.LWPCookieJar()    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))    urllib2.install_opener(opener)    args = {'pass':password, 'm':user,'loginstatus':loginstatus}    print 'Logining...'    req = urllib2.Request(url_login, urllib.urlencode(args))    jump = opener.open(req)    page = jump.read();    url = re.compile(r'<card id="start".*?ontimer="(.*?);').findall(page)[0]             #��ȡ��ת����    arg_t = re.compile(r't=(\d*)').findall(page)[0]    if url == '/im/login/login.action':                                                   #��¼ʧ��        print 'Login Failed!'        raw_input('Press any key to exit.')        return    else:        print 'Login Successfully!'    sendmsg = urllib2.Request(url_msg, urllib.urlencode({'msg':msg.decode('gbk').encode('utf-8')}))    finish = urllib2.urlopen(sendmsg)    if finish.geturl == 'http://f.10086.cn/im/user/sendMsgToMyself.action' :        print 'Send Failed!'    else:        print 'Send Successfully'    logout = urllib2.Request(url_logout + arg_t)    response = urllib2.urlopen(logout)                                                    #ע��    print 'Logout Successfully!'    #print response.read().decode('utf-8').encode('gbk')msg = raw_input('what do you want to say:')fetion(msg)

 

原创粉丝点击