NiceLeeのBlog 用爱发电 bilibili~

Python 使用分享接口实现QQ信息发送(四)

2020-06-10
nIceLee

阅读:


接上一篇

前言

半成品,模拟LOL页面登录(https://lol.qq.com),未完成。
因为lol登录后,转到前面分享页面会一直死循环跳转,没有动力再继续下去了…

# coding=utf-8
import requests
import re
import time
import execjs


def login(username, pwd):
    session = requests.session()
#     proxy = {
#         'http': '127.0.0.1:8888',
#         'https': '127.0.0.1:8888',
#     }
#     session.proxies = proxy
#     session.verify = False
    ###################
    url = "https://xui.ptlogin2.qq.com/cgi-bin/xlogin?proxy_url=https://game.qq.com/comm-htdocs/milo/proxy.html&appid=21000501&target=top&s_url=https%3A%2F%2Flol.qq.com%2Fmain.shtml&style=20&daid=8"
    headers = {
        'Host': 'xui.ptlogin2.qq.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Referer': 'https://lol.qq.com/main.shtml',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'TE': 'Trailers',
    }
    session.request(method='GET', url=url, headers=headers)
    cookies = requests.utils.dict_from_cookiejar(session.cookies)
    pt_login_sig = cookies['pt_login_sig']
    
    #####################
    url = "https://ssl.ptlogin2.qq.com/check?regmaster=&pt_tea=2&pt_vcode=1&uin=123456789&appid=21000501&js_ver=20032614&js_type=1&u1=https%3A%2F%2Flol.qq.com%2Fmain.shtml&r=0.8346944097860031&pt_uistyle=40&login_sig=" + pt_login_sig
    headers = {
        'Host': r'ssl.ptlogin2.qq.com',
        'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
        'Accept': r'*/*',
        'Accept-Language': r'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': r'gzip, deflate, br',
        'Connection': r'keep-alive',
        'Referer': r'https://xui.ptlogin2.qq.com/cgi-bin/xlogin?proxy_url=https://game.qq.com/comm-htdocs/milo/proxy.html&appid=21000501&target=top&s_url=https%3A%2F%2Flol.qq.com%2Fmain.shtml&style=20&daid=8',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'TE': 'Trailers',
    }
    result = session.request(method='GET', url=url, headers=headers).text
    result = result.replace('ptui_checkVC(', '').replace('\r', '').replace('\n', '')[:-1]
    temp = result.split(",")
    vcode = temp[1].replace("'", '')
    salt = str(temp[2].replace("'", ''))
    cookies = requests.utils.dict_from_cookiejar(session.cookies)
    print(cookies)
    ptvfsession = cookies['ptvfsession']
    ptdrvs = cookies['ptdrvs']
    
    #####################
    
    encryptedPwd = encrypt(pwd, salt, vcode)
    url = "https://ssl.ptlogin2.qq.com/login?"
    params = {
        "u":username,
        "verifycode":vcode,
        "pt_vcode_v1":"0",
        "pt_verifysession_v1":ptvfsession,
        "p":encryptedPwd,
        "pt_randsalt":"2",
        "u1":"https%3A%2F%2Flol.qq.com%2Fmain.shtml", #"https://lol.qq.com/main.shtml",
        "ptredirect":"1",
        "h":"1",
        "t":"1",
        "g":"1",
        "from_ui":"1",
        "ptlang":"2052",
        "action":"%d-%d-%d" % (2, len(pwd) + 1, int(round(time.time() * 1000))),
        "js_ver":"20032614",
        "js_type":"1",
        "login_sig":pt_login_sig,
        "pt_uistyle":"40",
        "aid":"21000501",
        "daid":"8",
        "ptdrvs":ptdrvs
    }
    param_str = ''
    for key in params:
        param_str += '%s=%s&'%(key, params[key])
    url += param_str
#     urlencode(params)
    print(url)
    headers = {
        'Host': 'ssl.ptlogin2.qq.com',
        'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
        'Accept': '*/*',
        'Accept-Language': r'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Referer': r'https://xui.ptlogin2.qq.com/cgi-bin/xlogin?proxy_url=https://game.qq.com/comm-htdocs/milo/proxy.html&appid=21000501&target=top&s_url=https%3A%2F%2Flol.qq.com%2Fmain.shtml&style=20&daid=8',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'TE': 'Trailers',
    }
    result = session.request(method='GET', url=url, headers=headers).text
    print(result)
    searchObj = re.search(r"(https://.*?)',", result)
    if searchObj:
        print("登录成功")
        url = searchObj.group(1)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding': 'gzip, deflate, br',
            'Connection': 'keep-alive',
            'Referer': 'https://xui.ptlogin2.qq.com/',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache',
            'TE': 'Trailers',
        }
        result = session.request(method='GET', url=url, headers=headers, allow_redirects=False).text
        print(result)
    session.close()

    
def encrypt(pwd, salt, vcode):
    with open('encrypt.js', 'r') as f:
        js_code = f.read()
    ctx = execjs.compile(js_code)
    result = ctx.call('encrypt', pwd, salt, vcode)
    print(result)
    return result

    
if __name__ == '__main__':
    login('username', 'password')
    encrypt("password", '\x00\x00\x00\x00\x07\x5b\xcd\x15', '!TRR')


encrypt.js此处


相似文章

内容
隐藏