1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python实现QQ机器人(自动登录 获取群消息 发送群消息)

python实现QQ机器人(自动登录 获取群消息 发送群消息)

时间:2023-06-10 00:05:33

相关推荐

python实现QQ机器人(自动登录 获取群消息 发送群消息)

python实现QQ机器人(自动登录,获取群消息,发送群消息)

版权声明: /zhujunxxxxx/article/details/29412297

一次偶然的机会我看见了一个群里的一个QQ号总是根据你所发的消息自动回复,当时很感觉到神奇,我知道可以模拟登录网站,没想到竟然也能模拟登录QQ,首先自己想到的就是如何实现模拟登录PC端的QQ, 开始研究了下,发现QQ所发送的包都很难理解。

于是就转到了网页版的QQ,因为在网页里可以捕获所有的请求从而更好的实现模拟功能!

首先第一步就是打开网页版的qq,打开浏览器的开发者模式 这里可以监控所有的请求!

打开登录界面的的时候

会捕捉到一个GET请求

https://ssl./check?uin=10588690&appid=1003903&js_ver=10080&js_type=0&login_sig=YW1ZUUsIU*7FepsR1blgEgcSVWeHCrNVVquTT1LZ0paOxZ-6xHtypEqNGoo-VELQ&u1=http%3A%2F%%2Floginproxy.html&r=0.5928007187321782

其中uin是你的QQ号

返回值是 ptui_checkVC('1','AAr4bdjMeh2hEa77PTuoHhqMTxbRqOp3','\x00\x00\x00\x00\x00\xa1\x92\x12');

其中1表示需要验证码 还有一种返回值 ptui_checkVC('0','!LJV','\x00\x00\x00\x00\x00\xa1\x92\x12') 这种表示是不需要的验证码的

defCheckVerify(self,uin):check="https://ssl./check?uin={uin}&appid=1003903&js_ver=10080&js_type=0&login_sig=YPD0P*wu2n8vW1OS2f7VfzvPf3Ku5vnkP4nzImF0GybR02fsKZdjGYB7f9R7nQRn&u1=http%3A%2F%%2Floginproxy.html&r=0.8179273759014904"check=check.replace('{uin}',uin)pattern=pile("ptui_checkVC\('(.*)','(.*)','(.*)'\);")result=self.Get(check)checked=pattern.search(result).groups()print'Step1:CheckVerify'returnchecked

获取验证码的方法

defGetVerify(self):#url='https://ssl./getimage?&uin='+str(self.QQ)+'&aid=1002101&0.45644426648505'+str(random.randint(10,99))verify="https://ssl./getimage?aid=1003903&r=0.6472875226754695&uin={QQ}&cap_cd=aSD-ZVcNEcozlZUurhNYhp-MBHf4hjbJ"verify=verify.replace('{QQ}',self.QQ)path=r"c:/verify/1.jpg"#data=urllib.urlretrieve(url,path)data=urllib2.urlopen(verify)localPic=open(r"c:/verify/1.jpg",'wb')localPic.write(data.read())localPic.close()data.close()

输入用户名和密码 还有验证码后发送一个GET请求

https://ssl./login?u=10588690&p=AB80CD3B6429D9660878E93058DD78BD&verifycode=TEYX&webqq_type=10&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=6-14-296574&mibao_css=m_webqq&t=1&g=1&js_type=0&js_ver=10080&login_sig=YW1ZUUsIU*7FepsR1blgEgcSVWeHCrNVVquTT1LZ0paOxZ-6xHtypEqNGoo-VELQ&pt_uistyle=5

这里面u代表账号 p代表密码 密码是经过一定算法加密的 verify是验证码

加密算法如下

defPasswordSecret(self,password,v1,v2,md5=True):ifmd5==True:password=self.PCMd5(password).upper()length=len(password)temp=''foriinrange(0,length,2):temp+=r'\x'+password[i:i+2]returnself.PCMd5(self.PCMd5(self.hex2asc(temp)+self.hex2asc(v2)).upper()+v1).upper()#md5加密函数defPCMd5(self,s):h=hashlib.md5()h.update(s)returnh.hexdigest()#16进制转字符defhex2asc(self,s):_str="".join(s.split(r'\x'))length=len(_str)data=''foriinrange(0,length,2):data+=chr(int(_str[i:i+2],16))returndata

然后是登录部分代码

defLogin(self,uin,pwd):#获取参数cheked=self.CheckVerify(uin)#加密密码#pwd=self.PasswordSecret(pwd,cheked[1],cheked[2])#pwd=self.PasswordSecret(pwd,r'AAST',r'\x00\x00\x00\x00\x00\xa1\x92\x12')loginurl="https://ssl./login?u={uin}&p={pwd}&verifycode={verify}&webqq_type=10&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=4-30-135914&mibao_css=m_webqq&t=1&g=1&js_type=0&js_ver=10080&login_sig=YPD0P*wu2n8vW1OS2f7VfzvPf3Ku5vnkP4nzImF0GybR02fsKZdjGYB7f9R7nQRn&pt_uistyle=5"loginurl=loginurl.replace('{uin}',uin)#loginurl=loginurl.replace('{pwd}',pwd)#loginurl=loginurl.replace('{verify}',cheked[1])#result=Get(loginurl)if(cheked[0]=="1"):#下载验证码self.GetVerify()image=Image.open(r"c:/verify/1.jpg")image.show()code=raw_input("verifycode:").strip()loginurl=loginurl.replace('{verify}',code.upper())pwd=self.PasswordSecret(pwd,r''+code.upper(),cheked[2])#pwd=self.PasswordSecret(pwd,cheked[1],cheked[2])else:loginurl=loginurl.replace('{verify}',cheked[1])pwd=self.PasswordSecret(pwd,cheked[1],cheked[2])loginurl=loginurl.replace('{pwd}',pwd)result=self.Get(loginurl,'ssl.','https://ui./cgi-bin/login?daid=164&target=self&style=5&mibao_css=m_webqq&appid=1003903&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%%2Floginproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=0514001',None)print'Step2:Login'pattern=pile("ptuiCB\('(.*)','(.*)','(.*)','(.*)','(.*)',\s'(.*)'\);")ret=pattern.search(result).groups()#获取必要的cookie否则第二次登陆会出错self.Get(ret[2])print'Step3:GetCookie'forcinself.cj:ifc.name=="ptwebqq":self.ptwebqq=c.valuereturnresult

登录成功后服务器会返回一串json数据

ptuiCB('0','0','http://ptlogin4./check_sig?pttype=1&uin=10588690&service=login&nodirect=0&ptsig=*ZwU0pfTmYP93Fbdt6uSzbbODzlZ0EY9g25PDge5zZU_&s_url=http%3A%2F%%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&f_url=&ptlang=2052&ptredirect=100&aid=1003903&daid=164&j_later=0&low_login_hour=0&regmaster=0&pt_login_type=1&pt_aid=0&pt_aaid=0&pt_light=0','0','登录成功!', '小竹');

第一个为0 就表示登录成功了 ,但是这并没有真正的登录成功

上面的返回值中的url是用来获取一个关键cookie的 那就是ptwebqq

然后进行第二次登录,这次才是真正的登录

http://d./channel/login2

请求如下

Accept:*/*

Accept-Encoding:gzip,deflate,sdch

Accept-Language:zh-CN,zh;q=0.8

Connection:keep-alive

Content-Length:244

Content-Type:application/x-www-form-urlencoded

Cookie:o_cookie=455910092; RK=fMEaWEZ0Qc; ts_last=/; ts_refer=/; ts_uid=4588821804; pgv_pvid=914251705; pgv_info=ssid=s3525422600&pgvReferrer=; verifysession=h02LeYrtarkWBZeSu_czkiczeNSNlDm7V1mCm-A5qatkwnHaNfgb2z46zH4X7OfyhFT7wH6LfschPvSLhDGXFA4eA**; ptui_loginuin=10588690; ptisp=cnc; ptcz=dace9cf90e7064a16ee56c8153273eff9f2de1d2827ba31f6571412ac18c50c3; ptwebqq=b21232ed3519839063d1c2ead8a8588c385d168097efdf88bc56e1a78be7dfb4; pt2gguin=o0010588690; uin=o0010588690; skey=@gmEO6N2JD; p_uin=o0010588690; p_skey=cZ5*kS-NOcXlD2Q0AEpJnmExwC2yA0g7jbTygpVFiA8_; pt4_token=1SyuJ39Eq6oKEwEhGIizeg__

Host:d.

Origin:http://d.

Referer:http://d./proxy.html?v=0331002&callback=1&id=2

User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36

Form Dataview sourceview URL encoded

r:{"status":"online","ptwebqq":"b21232ed3519839063d1c2ead8a8588c385d168097efdf88bc56e1a78be7dfb4","passwd_sig":"","clientid":"7963288","psessionid":null}

clientid:7963288

psessionid:null

其中的ptwebqq就是刚才我们从cookie中获取的

这部分代码是

defLogin2(self):try:url="http://d./channel/login2"postdata="r=%7B%22status%22%3A%22online%22%2C%22ptwebqq%22%3A%22{$ptwebqq}%22%2C%22passwd_sig%22%3A%22%22%2C%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3Anull%7D&clientid={$clientid}&psessionid=null"postdata=postdata.replace("{$ptwebqq}",self.ptwebqq)postdata=postdata.replace("{$clientid}",str(self.clientid))print'Step4:Login2'result=self.Post(url,postdata,QQRobot.HOST[0],QQRobot.REFERER[0],QQRobot.ORIGIN[0])retjson=json.loads(result)retjson=retjson["result"]returnretjsonexceptException,e:print"Login2error"+str(e)

第二次登陆成功后会返回一个

'''{"retcode":0,

"result":{

"uin":10588690,

"cip":1707901841,

"index":1075,

"port":59571,

"status":"online",

"vfwebqq":"c043f1f6ce5c3b76a4603ab60082668bef2dde0b987808f728e2071eb7c164eeb30fcd85c31018d2",

"psessionid":"8368046764001d636f6e6e7365727665725f77656271714031302e3133392e372e31363000006cb000001ae1036200a192126d0000000a40356c593742635175316d00000028c043f1f6ce5c3b76a4603ab60082668bef2dde0b987808f728e2071eb7c164eeb30fcd85c31018d2",

"user_state":0,

"f":0

}

}'''

这样的数据结构 其中0表示登陆成功

需要把这写数据保存下来 后面进行操作需要

登陆成功后我们就可以拉去群列表了

#获取群列表信息defGetGroupNameList(self,vfwebqq):try:url="http://s./api/get_group_name_list_mask2"postdata="r=%7B%22vfwebqq%22%3A%22{$vfwebqq}%22%7D"postdata=postdata.replace("{$vfwebqq}",vfwebqq)ret=self.Post(url,postdata,QQRobot.HOST[1],QQRobot.REFERER[1],QQRobot.ORIGIN[1])print'Step5:GetGroupList'retjson=json.loads(ret)retjson=retjson["result"]self.grouplist=retjsonforgroupinself.grouplist['gnamelist']:printgroup["code"],group["name"]exceptException,e:print"GetGroupNameListerror"+str(e)

#获取群成员信息defGetGroupInfo(self,gcode,vfwebqq):try:url="http://s./api/get_group_info_ext2?gcode={$gcode}&cb=undefined&vfwebqq={$vfwebqq}&t=1402069438458"url=url.replace("{$vfwebqq}",vfwebqq)url=url.replace("{$gcode}",str(gcode))ret=self.Get(url,QQRobot.HOST[1],QQRobot.REFERER[1],None)print"Step6:GetGroupInfo"retjson=json.loads(ret)retjson=retjson["result"]self.groupuserlist=retjsonexceptException,e:print"GetGroupInfoerror"+str(e)

#发送群消息defSendGroupMsg(self,groupid,msg,psessionid):try:#msg=u">:"+msg#msg=msg.strip()#urlmsg=quote(msg.encode('utf8'))#把普通字符串包裹起来stype="%5C%22{content}%5C%22"temp=""part=""forcinmsg:iftype(c)istypes.ListType:part=quote(str(c).strip().encode('utf8'))+"%2C"#part=part.replace("%20","")part=part.replace("%27","%5C%22")#把'换为\"part=part.replace("u","")#把u换为空temp+=partelse:temp+=stype.replace("{content}",quote(c.encode('utf8')))+"%2C"temp=temp[0:len(temp)-3]#urlmsg="%5C%228%5C%22"#"%5B%5C%22face%5C%22%2C13%5D"urlmsg=tempurl="http://d./channel/send_qun_msg2"msg_id=77860003#postdata="r=%7B%22group_uin%22%3A{$group_uin}%2C%22content%22%3A%22%5B%5C%22{$msg}%5C%22%2C%5C%22%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A{$msg_id}%2C%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%7D&clientid={$clientid}&psessionid={$psessionid}"#表情#postdata="r=%7B%22group_uin%22%3A{$group_uin}%2C%22content%22%3A%22%5B{$msg}%2C%5C%22%5C%5Cn%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A{$msg_id}%2C%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%7D&clientid={$clientid}&psessionid={$psessionid}"#字符postdata="r=%7B%22group_uin%22%3A{$group_uin}%2C%22content%22%3A%22%5B{$msg}%2C%5C%22%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A{$msg_id}%2C%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%7D&clientid={$clientid}&psessionid={$psessionid}"postdata=postdata.replace("{$group_uin}",str(groupid))postdata=postdata.replace("{$psessionid}",psessionid)postdata=postdata.replace("{$clientid}",str(self.clientid))postdata=postdata.replace("{$msg_id}",str(msg_id))postdata=postdata.replace("{$msg}",urlmsg)self.Post(url,postdata,QQRobot.HOST[0],QQRobot.REFERER[0],QQRobot.ORIGIN[0])exceptException,e:print"SendGroupMsgerror"+str(e)#print"sendmsg:"+str(msg)

心跳就是每隔一定时间向服务器请求一次 证明自己还在!

#心跳包defHeartBreak(self,psessionid):url="http://d./channel/poll2"postdata="r=%7B%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%2C%22key%22%3A0%2C%22ids%22%3A%5B%5D%7D&clientid={$clientid}&psessionid={$psessionid}"postdata=postdata.replace("{$clientid}",str(self.clientid))postdata=postdata.replace("{$psessionid}",psessionid)whileTrue:#每隔2秒发送心跳包ret=self.Post(url,postdata,QQRobot.HOST[0],QQRobot.REFERER[0],QQRobot.ORIGIN[0])try:retjson=json.loads(ret)retjson=retjson["result"]retjson=retjson[0]#print"heartbreak"if(retjson["poll_type"]=="group_message"):msg=retjson["value"]self.ProcessMsg(msg)exceptException,e:#print"HeartBreakerror"+str(e)passtime.sleep(2)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。