首页 > 编程 > Python > 正文

Python(Tornado)模拟登录小米抢手机

2020-02-23 04:56:10
字体:
来源:转载
供稿:网友
今天看到同事参与小米的抢购,几经数个星期的尝试,终于抢到了一台小米电视……看了一下小米的抢购流程,似乎可以用程序可破。于是想写点东西玩玩(你懂的……),第一步肯定是先得模拟登录小米帐号,当练手吧。
用 Python 来实现吧,由于是写一个Web应用,那么框架就选 Tornado。
首先是定义应用的 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 过去的数据,用 Fiddler 来嗅探一下:

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

需要构造的表单参数也很简单(已进行 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'

接下来函数也可以写出来了:
代码如下:
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)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表