The method of using Python to realize simple QQ single user robot _python

Source: Internet
Author: User
Tags extend

Using Python to achieve simple QQ single user robot method is as follows:

First, we look at the relevant agreement on 3GQQ:
To this, open a WAP-enabled browser, you can use the Firefox wmlbrowser plug-in, open FF, access to address https://addons.mozilla.org/zh-CN/firefox/search/?q= wmlbrowser&cat=all&x=17&y=11

Second, enter the 3GQQ of the Protocol analysis
3GQQ address is: Http://pt.3g.qq.com/s?aid=nLogin3gqq with the Wmlbrowser plug-in installed FF open the page, enable Firebug, you can implement monitoring the submitted data.

Third, the Source code section:

#coding: Utf-8 #基于python2.6 Version Development Import httplib,urllib,os,threading,re import sys reload (SYS) sys.setdefaultencoding ('
    UTF8 ') class pyqq:def __init__ (self): Self.reqindex = 0 #HTTP请求 def httprequest (self,method,url,data={}): Try: _urld = httplib.urlsplit (URL) conn = Httplib.
        Httpconnection (_urld.netloc,80,true,3) conn.connect () data = Urllib.urlencode (data) If method== ' get ': Conn.putrequest ("Get", url, None) conn.putheader ("Content-length", ' 0 ') elif method== ' Post ': Co  Nn.putrequest ("POST", url) conn.putheader ("Content-length", str (data)) Conn.putheader ("Content-type",
      
      "Application/x-www-form-urlencoded") Conn.putheader ("Connection", "Close") conn.endheaders ()
      If Len (data) >0:conn.send (data) F = conn.getresponse () Self.httpbody = F.read (). Encode (' UTF8 ') F.close () conn.close () except:self.httpbody= ' return self.hTtpbody #通过首尾获取字符串的内容 def getcon (self,start,end): Findex = Self.httpBody.find (start) if Findex = = -1:return
    None tmp = self.httpBody.split (start) Eindex = Tmp[1].find (end) If Eindex = = -1:return Tmp[1][0:] Else:return Tmp[1][0:eindex] #获取postfield的值 def getfield (self,fd): Keystart = ') #获取登陆验证码, and save to the current directory of Q Qcode.gif def getsafecode (self): url = Self.getcon (' Python_ implement simple QQ single user robot import URLLIB2 pager = Urllib2.urlop En (URL) data=pager.read () File=open (OS.GETCWD () + ' \qqcode.gif ', ' w+b ') file.write (data) file.close () ret Urn True #登陆QQ def login (self): SELF.QQ = raw_input (' Please enter QQ number: '. Encode (' GBK ')) Self.pwd = raw_input (' Enter password: '. Enc Ode (' GBK ')) S1back = Self.httprequest (' Post ', ' Http://pt.3g.qq.com/handleLogin ', {' R ': ' 240971315 ', ' QQ ': SELF.QQ, ' PWD ': self.pwd, ' toqqchat ': ' true ', ' q_from ': ', ' Modifyskey ': 0, ' Logintype ': 1} ' if S1back.find (' Please enter a CAPTCHA code ')!=-1:self.si D = Self.getfield (' sid ') sElf.hexpwd = Self.getfield (' hexpwd ') self.extend = Self.getfield (' extend ') Self.r_sid = Self.getfield (' R_sid ') Self.rip = Self.getfield (' RIP ') if Self.getsafecode (): Self.safecode = Raw_input (' Enter the verification code (Qqcod of this file in the same directory) e.gif): ' Else:print ' Verification Code loading error ' PostData = {' sid ': Self.sid, ' QQ ': self.qq, ' hexpwd ': self.hexpwd, ' H Exp ': ' True ', ' auto ': ' 0 ', ' logintitle ': ' Mobile Tencent net ', ' q_from ': ', ' modifyskey ': ' 0 ', ' q_status ': ' 27 ', ' R ': ' 1 ', ' logintype ': ' 1 ', ' prev_url ': ' Ten ', ' extend ': self.extend, ' r_sid ': self.r_sid, ' bid_code ': ', ' bid ': '-1 ', ' toqqchat ': ' True ', ' RIP ': self.rip, ' verify ': self.safecode,} s1back = Self.httprequest (' Post ', ' Http://pt.3g.qq.com/handle Login ', postdata ' self.sid = Self.getcon (' sid= ', ' & ') #print Self.sid print ' landed success '. Encode (' GBK ') se Lf.getmsgfun () #定时获取消息 def getmsgfun (self): Self.reqindex = self.reqindex + 1 s2back = self.httprequest (' Get ', ' http://q32.3g.qq.com/G/s?aid=nqqchatmain&sid= ' +self.sid ' if S2back.find (' alt= chat '/> (')!=-1: #有新消息, request to get message page S3back = self . HttpRequest (' Get ', ' http://q32.3g.qq.com/g/s?sid= ' + self.sid + ' &aid=nqqchat&saveurl=0&r=1310115753
      &g_f=1653&on=1 ') #消息发起者的昵称 if S3back.find (' title= temporary Session ')!=-1: _fromname = ' temporary conversation ' else: _fromname = Self.getcon (' title= "and ', ' chat ') #消息发起者的QQ号 _fromqq = Self.getcon (' num ' value= ')
      '/> ') #消息内容 _msg_tmp = Self.getcon (' saveurl=0 ' > Hint) ', \ ' <input name= ' msg ' \ ') CRLF = ' \ n ' If _msg_tmp.find (' \ r \ n ')!=-1:crlf = ' \ r \ n ' _msg = Re.findall (R ' (. +) ' + CRLF + ' (. +) ', _msg_tmp) F
    
    or _data in _msg:self.getmsg ({' QQ ': _fromqq, ' Nick ': _fromname, ' time ': _data[0], ' msg ': Str (_data[1]). Strip ())
      If self.reqindex>=30: #保持在线 _url = ' http://pt5.3g.qq.com/s?aid=nLogin3gqqbysid&3gqqsid= ' +self.sid
   Self.httprequest (' Get ', _url)   Self.reqindex = 0 T = Threading. Timer (2.0,self.getmsgfun) T.start () #发送消息 #qq target QQ #msg send content def sendmsgfun (self,qq,msg): msg = Unicode ( msg, ' UTF8 '). Encode (' UTF8 ') postdata = {' sid ': Self.sid, ' on ': ' 1 ', ' saveurl ': ' 0 ', ' saveurl ': ' 0 ', ' u ': QQ, ' msg ': str (msg), } s1back = Self.httprequest (' Post ', ' http://q16.3g.qq.com/g/s?sid= ' + self.sid + ' &aid=sendmsg&tfor=qq '), postdata) print ' sends a message to '. Encode (' GBK '), QQ, ' success '. Encode (' GBK ') #收到消息的接口, overload or override this method Def getmsg (self,data): Print D ata[' time '], "received". Encode (' GBK '), data[' Nick '].encode (' GBK '), "(" data[' QQ ', ")," new Message ". Encode (' GBK '),": ", data[' msg '] . Encode (' GBK ') self.sendmsgfun (data[' QQ '],data[' Nick ']+ '), test message. ') #+ data[' msg ']) QQ = PYQQ () qq.login ()

So far, the robot function can be realized!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.