Python(Tornado)模擬登錄小米搶手機(jī)
更新時間:2013年11月12日 09:18:41 作者:
用Python(Tornado)模擬登錄小米帳號,搶小米手機(jī)
今天看到同事參與小米的搶購,幾經(jīng)數(shù)個星期的嘗試,終于搶到了一臺小米電視……看了一下小米的搶購流程,似乎可以用程序可破。于是想寫點(diǎn)東西玩玩(你懂的……),第一步肯定是先得模擬登錄小米帳號,當(dāng)練手吧。
用 Python 來實(shí)現(xiàn)吧,由于是寫一個Web應(yīng)用,那么框架就選 Tornado。
首先是定義應(yīng)用的 URL:
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
(r"/mibuy/", MiBuyHandler),
],**settings)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
接下來就是尋找需要 post 過去的數(shù)據(jù),用 Fiddler 來嗅探一下:

也就是說,POST 的地址是 https://account.xiaomi.com/pass/serviceLoginAuth2

需要構(gòu)造的表單參數(shù)也很簡單(已進(jìn)行 URL 編碼):passToken=&user=www.nowamagic.net&pwd=password&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2BgLdeyOsdMhwV0Xg%3D。即:
post_data = urllib.urlencode({'passToken':'', 'user': 'www.nowamagic.net', 'pwd': 'password', 'callback':'https://account.xiaomi.com', 'sid':'passport', 'qs':'%3Fsid%3Dpassport', 'hidden':'', '_sign':'KKkRvCpZoDC+gLdeyOsdMhwV0Xg='})
path = 'https://account.xiaomi.com/pass/serviceLoginAuth2'
接下來函數(shù)也可以寫出來了:
class MiBuyHandler(tornado.web.RequestHandler):
def get(self):
cj = cookielib.CookieJar()
post_data = urllib.urlencode({'passToken':'', 'user': 'www.nowamagic.net', 'pwd': 'password', 'callback':'https://account.xiaomi.com', 'sid':'passport', 'qs':'%3Fsid%3Dpassport', 'hidden':'', '_sign':'KKkRvCpZoDC+gLdeyOsdMhwV0Xg='})
path = 'https://account.xiaomi.com/pass/serviceLoginAuth2'
cookieHandle = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookieHandle)
#opener.addheaders = [('User-agent', 'Opera/9.23')]
urllib2.install_opener(opener)
req = urllib2.Request(path, post_data)
response = urllib2.urlopen(req)
html = response.read()
self.render("mibuy.html",message=html)
如何需要把 cookie 打印出來,直接 print cj 就可以看到 cookie 的內(nèi)容。
接下來的事情貌似也很簡單,就是解析 hdcontrol (URL:http://tc.hd.xiaomi.com/hdget?callback=hdcontrol) 這個 json。
hdcontrol(
{
stime: 1383645496,
status: {
allow: true,
miphone: {
hdurl: "",
duration: null,
hdstop: true,
reg: true,
pmstart: false,
hdstart: false
},
mibox: {
hdurl: "",
duration: null,
hdstop: true,
reg: true,
pmstart: false,
hdstart: false
},
mitv: {
hdurl: "",
duration: null,
hdstop: true,
reg: false,
pmstart: false,
hdstart: false
}
}
})
當(dāng) allow 為 true 的時候,hdurl 會有值,比如 ?_a=20131105_phone_a212a2b30e5&_op=choose&_s=72b686828&_m=1 之類的,這個就是真實(shí)的搶購地址,直接訪問這個地址應(yīng)該就不用再點(diǎn)排隊的按鈕。僅當(dāng)拋磚引玉,懂程序的各位都該知道怎么辦了吧……
僅僅適用于目前(2013年11月),后續(xù)小米那邊可能會改變一些規(guī)則。
用 Python 來實(shí)現(xiàn)吧,由于是寫一個Web應(yīng)用,那么框架就選 Tornado。
首先是定義應(yīng)用的 URL:
復(fù)制代碼 代碼如下:
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
(r"/mibuy/", MiBuyHandler),
],**settings)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
接下來就是尋找需要 post 過去的數(shù)據(jù),用 Fiddler 來嗅探一下:

也就是說,POST 的地址是 https://account.xiaomi.com/pass/serviceLoginAuth2

需要構(gòu)造的表單參數(shù)也很簡單(已進(jìn)行 URL 編碼):passToken=&user=www.nowamagic.net&pwd=password&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2BgLdeyOsdMhwV0Xg%3D。即:
復(fù)制代碼 代碼如下:
post_data = urllib.urlencode({'passToken':'', 'user': 'www.nowamagic.net', 'pwd': 'password', 'callback':'https://account.xiaomi.com', 'sid':'passport', 'qs':'%3Fsid%3Dpassport', 'hidden':'', '_sign':'KKkRvCpZoDC+gLdeyOsdMhwV0Xg='})
path = 'https://account.xiaomi.com/pass/serviceLoginAuth2'
接下來函數(shù)也可以寫出來了:
復(fù)制代碼 代碼如下:
class MiBuyHandler(tornado.web.RequestHandler):
def get(self):
cj = cookielib.CookieJar()
post_data = urllib.urlencode({'passToken':'', 'user': 'www.nowamagic.net', 'pwd': 'password', 'callback':'https://account.xiaomi.com', 'sid':'passport', 'qs':'%3Fsid%3Dpassport', 'hidden':'', '_sign':'KKkRvCpZoDC+gLdeyOsdMhwV0Xg='})
path = 'https://account.xiaomi.com/pass/serviceLoginAuth2'
cookieHandle = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookieHandle)
#opener.addheaders = [('User-agent', 'Opera/9.23')]
urllib2.install_opener(opener)
req = urllib2.Request(path, post_data)
response = urllib2.urlopen(req)
html = response.read()
self.render("mibuy.html",message=html)
如何需要把 cookie 打印出來,直接 print cj 就可以看到 cookie 的內(nèi)容。
接下來的事情貌似也很簡單,就是解析 hdcontrol (URL:http://tc.hd.xiaomi.com/hdget?callback=hdcontrol) 這個 json。
復(fù)制代碼 代碼如下:
hdcontrol(
{
stime: 1383645496,
status: {
allow: true,
miphone: {
hdurl: "",
duration: null,
hdstop: true,
reg: true,
pmstart: false,
hdstart: false
},
mibox: {
hdurl: "",
duration: null,
hdstop: true,
reg: true,
pmstart: false,
hdstart: false
},
mitv: {
hdurl: "",
duration: null,
hdstop: true,
reg: false,
pmstart: false,
hdstart: false
}
}
})
當(dāng) allow 為 true 的時候,hdurl 會有值,比如 ?_a=20131105_phone_a212a2b30e5&_op=choose&_s=72b686828&_m=1 之類的,這個就是真實(shí)的搶購地址,直接訪問這個地址應(yīng)該就不用再點(diǎn)排隊的按鈕。僅當(dāng)拋磚引玉,懂程序的各位都該知道怎么辦了吧……
僅僅適用于目前(2013年11月),后續(xù)小米那邊可能會改變一些規(guī)則。
相關(guān)文章
python中slice參數(shù)過長的處理方法及實(shí)例
在本篇文章里小編給大家分享了一篇關(guān)于python中slice參數(shù)過長的處理方法及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2020-12-12
Python3操作Excel文件(讀寫)的簡單實(shí)例
這篇文章主要給大家介紹了關(guān)于Python3操作Excel文件(讀寫)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python3具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
pandas讀取csv格式數(shù)據(jù)時header參數(shù)設(shè)置方法
本文主要介紹了pandas讀取csv格式數(shù)據(jù)時header參數(shù)設(shè)置方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
python爬蟲反爬之圖片驗證功能實(shí)現(xiàn)
這篇文章主要介紹了python爬蟲反爬之圖片驗證功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03
python按順序重命名文件并分類轉(zhuǎn)移到各個文件夾中的實(shí)現(xiàn)代碼
這篇文章主要介紹了python按順序重命名文件并分類轉(zhuǎn)移到各個文件夾中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
python中三種輸出格式總結(jié)(%,format,f-string)
在Python語言編程中,我們會與字符串打交道,那務(wù)必會輸出字符串來查看字符串的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于python中三種輸出格式的相關(guān)資料,三種格式分別是%,format,f-string,需要的朋友可以參考下2022-03-03

