Python(Tornado)模擬登錄小米搶手機
更新時間:2013年11月12日 09:18:41 作者:
用Python(Tornado)模擬登錄小米帳號,搶小米手機
今天看到同事參與小米的搶購,幾經(jīng)數(shù)個星期的嘗試,終于搶到了一臺小米電視……看了一下小米的搶購流程,似乎可以用程序可破。于是想寫點東西玩玩(你懂的……),第一步肯定是先得模擬登錄小米帳號,當(dāng)練手吧。
用 Python 來實現(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 之類的,這個就是真實的搶購地址,直接訪問這個地址應(yīng)該就不用再點排隊的按鈕。僅當(dāng)拋磚引玉,懂程序的各位都該知道怎么辦了吧……
僅僅適用于目前(2013年11月),后續(xù)小米那邊可能會改變一些規(guī)則。
用 Python 來實現(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 之類的,這個就是真實的搶購地址,直接訪問這個地址應(yīng)該就不用再點排隊的按鈕。僅當(dāng)拋磚引玉,懂程序的各位都該知道怎么辦了吧……
僅僅適用于目前(2013年11月),后續(xù)小米那邊可能會改變一些規(guī)則。
相關(guān)文章
一文教你用Python中progress庫實現(xiàn)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了如何通過Python中的progress庫實現(xiàn)進(jìn)度條的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03Python數(shù)據(jù)結(jié)構(gòu)之列表與元組詳解
序列是Python中最基本的數(shù)據(jù)結(jié)構(gòu)。序列中的每個元素都分配一個數(shù)字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推,元組與列表類似,不同之處在于元組的元素不能修改。元組使用小括號,列表使用方括號2021-10-10windows下Python實現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
這篇文章主要介紹了windows下Python實現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法,結(jié)合實例形式較為詳細(xì)的分析了Python實現(xiàn)將pdf轉(zhuǎn)換為png格式的相關(guān)模塊、使用方法與相關(guān)注意事項,需要的朋友可以參考下2017-07-07如何將Pycharm中Terminal使用Powershell作為終端
這篇文章主要介紹了如何將Pycharm中Terminal使用Powershell作為終端問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05