作者: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.') return else: 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 cookielib import urllib import urllib2 import re url_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)
