Journal

Manipulating QQ with Python

2010·12·31

Machine-translated from Chinese.  ·  Read original

The New Year in the United States is approaching, and my fellow students in China have already ushered in the new year. I’m taking advantage of the time difference to publish a technical article on the last day of 2010.

Last night, I stumbled upon an article by Create Chen that implemented QQ login using the HTTP-based QQ API. The author provided the implementation in C# and conducted a detailed analysis of the protocol. (URL1 URL2)

By controlling QQ through HTTP POST actions, we can develop many interesting applications. For example, we can develop a QQ message bomber (hehe :) ), create a QQ robot, and so on.

I reimplemented the method described in the article using my preferred Python, and here is the Python code. The code only implements the basic functionality and does not process the returned information. If you’re interested, you can combine Create Chen’s article to process the returned information on your own.

Here is the code:

import urllib,httplib,md5,time from time import sleep class qq: def init(self,qq="",pwd=""): self.pwd=md5.new(pwd).hexdigest() self.headers="" self.qq=qq

def getdata(self):
    self.conn=httplib.HTTPConnection("tqq.tencent.com:8000")
    self.conn.request("POST","",self.headers)
    response=self.conn.getresponse()
    print response.read().decode('utf-8').encode("cp936")
    sleep(1)
    self.conn.close()

def Login(self):
    self.headers=("VER=1.1&CMD;=Login&SEQ;="+\
                   str(int(time.time()*100)%(10**5))+"&UIN;="+\
                   self.qq+"&PS;="+\
                   self.pwd+\
                   "&M5;=1&LC;=9326B87B234E7235")
    self.getdata()

def GetInfo(self,friend=""):
    self.headers=("VER=1.1&CMD;=GetInfo&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq+"&LV;=2&UN;="+\
                  friend)
    self.getdata()
    
def AddToList(self,friend=""):
    self.headers=("VER=1.1&CMD;=AddToList&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq+"&UN;="+\
                  friend)
    self.getdata()

#agree_Type = 0 agree
#agree_Type = 1 deny
def Ack_AddToList(self,fri_Num,agree_Type):
    self.headers=("VER=1.1&CMD;=Ack_AddToList&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq+"&UN;="+\
                  fri_Num+"&CD;="+agree_Type+"&RS;=")
    self.getdata()
                 
def SendMsg(self,friend="",msg=""):
    self.headers=("VER=1.1&CMD;=CLTMSG&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq+"&UN;="+\
                  friend+"&MG;="+\
                  msg.decode("cp936").encode('utf-8'))
    self.getdata()
                 
def GetMsg(self):
    self.headers=("VER=1.1&CMD;=GetMsgEx&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq)
    self.getdata()

def Query_Stat(self):
    self.headers=("VER=1.1&CMD;=Query_Stat&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq+"&TN;=50&UN;=0")
    self.getdata()

def List(self):
    self.headers=("VER=1.1&CMD;=List&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq+"&TN;=160&UN;=0")
    self.getdata()

#stat = 10 online
#stat = 20 offline
#stat = 30 busy
def Change_Stat(self,stat=""):
    self.headers=("VER=1.1&CMD;=Change_Stat&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq+"&ST;="+stat)
    self.getdata()

def Logout(self):
    self.headers=("VER=1.1&CMD;=Logout&SEQ;="+\
                  str(int(time.time()*100)%(10**5))+"&UIN;="+\
                  self.qq)
    self.getdata()

test = qq(‘9918xxxx’,‘xxxxx’) test.Login() print “OK” #i =0 #while i<5:

print i

sleep(2)

test.SendMsg(‘xxxxxx’,“I am robot”)

i = i+1

i = 0 while i<10: print i test.Change_Stat(‘10’) sleep(1) test.Change_Stat(‘30’) sleep(1) test.Change_Stat(‘20’) sleep(1) i = i + 1 test.Logout()

代码下载地址:http://www.darlingtree.com/download/qqlogin.tar.gz 祝大家新年快乐!

留 · 言