This article mainly introduces how to simulate webqq login using Python, which has some reference value, if you are interested, refer to this example code for simulating webqq login using python. The Code is as follows:
Import hashlibfrom urllib import request, parsefrom http import cookiejarimport re, random, timeimport threading as thimport json. encoder as json_encodeimport json. decoder as json_decodeclass QQ: "" Login QQ "" _ headers = {'user-agent': 'mozilla/5.0 (Windows NT 6.1; WOW64) appleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.9 Safari/534.30 ', \ 'Referer ':' http://ui.ptlogin2.qq.com/cgi-bin/login?target=self&style=5&mibao_css=m_webqq&appid=1003903&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fwebqq.qq.com%2Floginproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20110909003 '\} _ Cookiepath = 'C: \ Users \ dingyangfan \ Desktop \ php \ tt.txt '_ qq = None _ pswd = None _ verifycode = None _ clientid = 21628014 _ http = {} _ psessionid = ''_ ptwebqq ='' _ vfwebqq = ''_ skey ='' _ poll2 = None _ get_msg_tip = None _ rc = 0 _ send_num = 31330000 def _ init _ (self): self. httpproess () self. _ Login () pass def _ preprocess (self, password, verifycode): "" QQ password encryption "return hashlib. md5 (self. _ md5_3 (password ). encode ('utf-8') + (verifycode ). upper ()). encode ('utf-8 ')). hexdigest (). upper () pass def _ md5_3 (self, str): "qqpassword md5_3" return hashlib. md5 (hashlib. md5 (hashlib. md5 (str ). digest ()). digest ()). hexdigest (). upper () pass def httpproess (self): "initialization simulation process" "self. _ http ['cj '] = cookiejar. mozillaCookieJar (self. _ cookiepath) self. _ http ['opener'] = request. build_opener (request. HTTPCookieProcessor (self. _ http ['cj ']) return self. _ http pass def _ request (self, url, method = 'get', data ={}, savecookie = False ): "request url" if (method ). upper () = 'post': data = parse. urlencode (data ). encode ('utf-8') self. _ http ['req'] = request. request (url, data, self. _ headers) else: self. _ http ['req'] = request. request (url = url, headers = self. _ headers) fp = self. _ http ['opener']. open (self. _ http ['req']) try: str = fp. read (). decode ('utf-8') encoding t UnicodeDecodeError: str = fp. read () if savecookie = True: self. _ http ['cj ']. save (ignore_discard = True, ignore_expires = True) fp. close () return str pass def _ getcookies (self, name): fp = open (self. _ cookiepath) fp. seek (130) for read in fp. readlines (): str = read. split (name) if len (str) = 2: fp. close () return str [1]. strip () fp. close () return None pass def _ getverifycode (self): "" @ url: http://ptlogin2.qq.com/check?uin= 644826377 & appid = 1003903 & r = 0.56373973749578 "" urlv =' http://ptlogin2.qq.com/check?uin= '+ (' % S' % self. _ qq) + '& appid = 1003903 & r =' + ('% s' % random. random (). random () str = self. _ request (url = urlv, savecookie = True) str = re. findall (R' \ d | (? <= \ ') [A-zA-Z0-9 \!] {4} ', str) return str pass def _ request_login (self): "@ url: http://ptlogin2.qq.com/login @ Params: {u: 644826377 p: 73DA5C1145E0F82247F60B3A17B89E6A verifycode :! S10 webqq_type: 10 remember_uin: 1 login2qq: 1 aid: 1003903 u1: http://webqq.qq.com/ Loginproxy.html? Login2qq = 1 & webqq_type = 10 h: 1 ptredirect: 0 ptlang: 2052 from_ui: 1 pttype: 1 dumy: fp: loginerroralert action: 1-24-62651 mibao_css: m_webqq} "" urlv =' http://ptlogin2.qq.com/login ? U = '+ (' % s' % self. _ qq) + '&' + 'P = '+ (' % s' % self. _ pswd) + '& verifycode =' + ('% s' % self. _ verifycode [1]) + '& remember_uin = 1 & aid = 000000' + "percent % 3 Fstrong % 3 Dtrue" +' & h = 1 & ptredirect = 0 & ptlang = 1003903 & from_ui = 1 & pttype = 1 & dumy = & fp = loginerroralert 'str = self. _ request (url = urlv, savecookie = True) if str. find ('logon successful ')! =-1: # execute the second logon self. _ ptwebqq = self. _ getcookies ('ptwebqq') self. _ skey = self. _ getcookies ('skey') self. _ request_post () elif str. find ('incorrect ')! =-1: print ('the account or password you entered is incorrect. Please enter it again. ') Else: print ('logon failed') pass def _ request_post (self ):''' http://d.web2.qq.com/channel/login2 R: {"status": "online", "ptwebqq": "success", "passwd_sig": "", "clientid": "21628014", "psessionid ": null} clientid: 21628014 psessionid: null ''' self. _ headers. update ({'Referer ':' http://d.web2.qq.com/proxy.html?v=20110331002&callback=2 '}) A = {'status': 'online', 'ptwebqq': self. _ getcookies ('ptwebqq'), 'passwd _ sig ': '', 'clientid': self. _ clientid, 'psessionid': 'null'} array = {'r': json_encode.JSONEncoder (). encode (a), 'clientid': self. _ clientid, 'psessionid': 'null'} url =' http://d.web2.qq.com/channel/login2 'Str = self. _ request (url, 'post', array) str = json_decode.JSONDecoder (). decode (str) self. _ psessionid = str ['result'] ['psessionid'] self. _ vfwebqq = str ['result'] ['vfwebqq'] self. _ get_friend_info2 () self. _ get_user_friends2 () self. _ get_group_name_list_mask2 () self. _ poll2 _ () self. _ get_msg_tip _ () pass def _ poll2 _ (self): "" I don't know what to do. It is connected once every minute. It is a persistent connection and receives a message @ url: http://d.web2.qq.com/channel/poll2 R: {"clientid": "9467930", "psessionid": "clientid", "key": 0, "ids": []} clientid: 9467930 psessionid: parameters "" self. _ headers. update ({'Referer ':' http://d.web2.qq.com/proxy.html?v=20110331002&callback=2 '}) Urlv =' http://d.web2.qq.com/channel/poll2 'A = {'clientid': self. _ clientid, 'psessionid': self. _ psessionid, 'key': 0, 'kids': []} array = {'r': json_encode.JSONEncoder (). encode (a), 'clientid': self. _ clientid, 'psessionid': self. _ psessionid} self. _ poll2 = self. _ request (url = urlv, method = 'post', data = array) str = json_decode.JSONDecoder (). decode (self. _ poll2) print (str) if str ['retcode'] = 0: if str ['result'] [0] ['Poll _ type'] = 'message': self. _ message (str ['result'] [0] ['value'] ['from _ uin']) elif str ['result'] [0] ['Poll _ type'] = 'group _ message': self. _ group_message (str ['result'] [0] ['value'] ['from _ uin']) pass t1 = th. timer (1, self. _ poll2 _) t1.start () pass def _ get_msg_tip _ (self): "" # I don't know what it is. I always request @ url: http://webqq.qq.com/ Web2/get_msg_tip? Uin = & tp = 1 & id = 0 & retype = 1 & rc = 64 & lv= 2 & t = 1315746772886 "" self. _ headers. update ({'Referer ':' http://webqq.qq.com/ '}) Self. _ rc + = 1 num = 100 + self. _ rc t = '% s' %' % d' % time. time () + '% s' % num urlv =' http://webqq.qq.com/ Web2/get_msg_tip? Uin = & tp = 1 & id = 0 & retype = 1 & rc = '+' % s' % self. _ rc + '& lv = 3 & t =' + t self. _ get_msg_tip = self. _ request (urlv) print (self. _ get_msg_tip) t2 = th. timer (60, self. _ get_msg_tip _) t2.start () pass def _ get_friend_info2 (self): ''' @ url: http://s.web2.qq.com/api/get_friend_info2?tuin= Self. _ qq & verifysession = & code = & vfwebqq = self. _ vfwebqq ''' self. _ headers. update ({'Referer ':' http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=2 '}) Url =' http://s.web2.qq.com/api/get_friend_info2?tuin= '+ Self. _ qq + '& verifysession = & code = & vfwebqq =' + self. _ vfwebqq + '& t = % s' %' % d' % time. time () + '000000' str = self. _ request (url) print (str) pass def _ get_user_friends2 (self): ''' @ url: http://s.web2.qq.com/api/get_user_friends2 '''Self. _ headers. update ({'Referer ':' http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=2 '}) Url =' http://s.web2.qq.com/api/get_user_friends2 'A = {'H': 'hello', 'vfwebqq': self. _ vfwebqq} array = {'r': json_encode.JSONEncoder (). encode (a)} str = self. _ request (url, 'post', array) print (str) pass def _ get_group_name_list_mask2 (self): ''' @ url: http://s.web2.qq.com/api/get_group_name_list_mask2 '''Self. _ headers. update ({'Referer ':' http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=2 '}) Url =' http://s.web2.qq.com/api/get_group_name_list_mask2 'A = {'vfwebqq': self. _ vfwebqq} array = {'r': json_encode.JSONEncoder (). encode (a)} str = self. _ request (url, 'post', array) print (str) pass def _ send_message (self, uid, msg): ''' @ url: http://d.web2.qq.com/channel/send_buddy_msg2 R: {"to": 3023379661, "face": 180, "content": "[\" Haha \ ", \" \ n [prompt: this user is using WebQQ: http://webqq.qq.com/ ] \ ", [\" Font \ ", {\" name \ ": \" \ ", \" size \ ": \" 10 \", \ "style \": [000000, 0], \ "color \": \ "31330001 \"}] "," msg_id ":," clientid ": "76133590", "psessionid": "s"} clientid: 76133590 psessionid: s Referer: http://d.web2.qq.com/proxy.html?v=20110331002&callback=2 {"Retcode": 0, "result": "OK"} ''' self. _ send_num + = 1 msg = "[\" "+ msg +" \ ", [\" font \ ", {\" name \ ": \" \", \ "size \": \ "10 \", \ "style \": [0, 0], \ "color \": \ "000000 \"}] "self. _ headers. update ({'Referer ':' http://d.web2.qq.com/proxy.html?v=20110331002&callback=2 '}); Url =' http://d.web2.qq.com/channel/send_buddy_msg2 'A = {'to': uid, 'face': 180, 'content': msg, 'msg _ id': self. _ send_num, 'clientid': self. _ clientid, 'psessionid': self. _ psessionid} array = {'r': json_encode.JSONEncoder (). encode (a), 'clientid': self. _ clientid, 'psessionid': self. _ psessionid} str = self. _ request (url, 'post', array) print (str) pass def _ message (self, uid): ''' {"retcode": 0, "result": [{"poll_type": "message", "value": {"msg_id": 13013, "from_uin": 3023379661, "to_uin": 644826377, "msg_id2": 503935, "msg_type": 9, "reply_ip": 176752345, "time": 1316143960, "content": [["font", {"size ": 13, "color": "000000", "style": [0, 0], "name": "\ u5B8B \ u4F53"}], "\ u4E0D \ u662F \ u5427 \ u3002"]} '''self. _ send_message (uid, 'I am a robot % s' % time. time () pass def _ send_group_message (self, gid, msg): ''' @ url: http://d.web2.qq.com/channel/send_qun_msg2 R: {"group_uin": 1132101900, "content": "[\" HAHAHA, test \ ", \" \ n [prompt: This user is using WebQQ: http://webqq.qq.com/ ] \ ", [\" Font \ ", {\" name \ ": \" \ ", \" size \ ": \" 10 \", \ "style \": [000000, 0], \ "color \": \ "31330002 \"}] "," msg_id ":," clientid ": "76133590", "psessionid": "a"} clientid: 76133590 psessionid: a Referer: http://d.web2.qq.com/proxy.html?v=20110331002&callback=2 {"Retcode": 0, "result": "OK"} ''' self. _ send_num + = 1 msg = "[\" "+ msg +" \ ", [\" font \ ", {\" name \ ": \" \", \ "size \": \ "10 \", \ "style \": [0, 0], \ "color \": \ "000000 \"}] "self. _ headers. update ({'Referer ':' http://d.web2.qq.com/proxy.html?v=20110331002&callback=2 '}); Url =' http://d.web2.qq.com/channel/send_qun_msg2 'A = {'group _ uin': gid, 'content': msg, 'msg _ id': self. _ send_num, 'clientid': self. _ clientid, 'psessionid': self. _ psessionid} array = {'r': json_encode.JSONEncoder (). encode (a), 'clientid': self. _ clientid, 'psessionid': self. _ psessionid} str = self. _ request (url, 'post', array) print (str) pass def _ group_message (self, gid): ''' {"retcode": 0, "result": [{"poll_type": "group_message", "value": {"msg_id": 8044, "from_uin": 1132101900, "to_uin": 644826377, "msg_id2": 178306, "msg_type": 43, "reply_ip": 2886742214, "group_code": 317106137, "send_uin": 3023379661, "seq": 10654, "time ": 1316143836, "info_seq": 32946855, "content": [["font", {"size": 13, "color": "000000", "style, 0], "name": "\ u5B8B \ u4F53"}], "\ u5DE5"]} '''self. _ send_group_message (gid, 'I am a robot % s' % time. time () pass def _ Login (self): "qq Login" self. _ qq = input ('qq: ') self. _ pswd = input ('qq password: ') self. _ qq = self. _ qq. strip () self. _ pswd = self. _ pswd. strip () self. _ verifycode = self. _ getverifycode () self. _ pswd = self. _ preprocess (self. _ pswd, # password \ '% s' % self. _ verifycode [1] # Verification Code \) self. _ request_login () pass s = QQ ()
The above is all the content of this article. I hope it will help you learn python programming.