|
#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 ':
Conn.putrequest ("POST", url)
Conn.putheader ("Content-length", str (len (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 (' GBK ')
F.close ()
Conn.close ()
Except
Self.httpbody= '
Return Self.httpbody
#HTTP请求的pycurl版本, and choose one of the above procedures
def httprequest_ (self,method,url,data={}):
Import Pycurl,stringio
c = Pycurl. Curl ()
C.setopt (Pycurl. Url,url)
If method== ' post ':
Import Urllib
C.setopt (C.postfields, Urllib.urlencode (data))
C.FP = Stringio.stringio ()
C.setopt (Pycurl. Writefunction,c.fp.write)
C.perform ()
Self.httpbody = C.fp.getvalue (). Encode (' GBK ')
Del C.FP
C.close ()
c = None
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 = ' <postfield name= ' + str (FD) + ' value= '
Return Self.getcon (Keystart, '/> ')
#获取登陆验证码, and save to the qqcode.gif of the current directory
def getsafecode (self):
url = Self.getcon (' Import Urllib2
Pager = Urllib2.urlopen (URL)
Data=pager.read ()
File=open (OS.GETCWD () + ' qqcode.gif ', ' w+b ')
File.write (data)
File.close ()
Return True
#登陆QQ
def login (self):
SELF.QQ = raw_input (' Please enter QQ number: ')
Self.pwd = raw_input (' Please enter password: ')
S1back = Self.httprequest (' Post ', ' Http://pt.3g.qq.com/handleLogin ', {' R ': ' 324525157 ', ' QQ ': self.qq, ' pwd ': self.pwd, ' Toqqchat ': ' true ', ' q_from ': ', ' Modifyskey ': 0, ' Logintype ': 1}
If S1back.find (' Please enter a CAPTCHA code ')!=-1:
Self.sid = 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 (' Please enter the verification code (qqcode.gif in the same directory as this file): ')
Else
print ' Verification Code load error '
PostData = {' sid ': Self.sid, ' QQ ': self.qq, ' hexpwd ': self.hexpwd, ' hexp ': ' True ', ' auto ': ' 0 ',
' Logintitle ': ' Mobile Tencent net ', ' q_from ': ', ' modifyskey ': ' 0 ', ' q_status ': ' 10 ',
' R ': ' 271 ', ' 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/handleLogin ', postdata)
Self.sid = Self.getcon (' sid= ', ' & ')
print ' Login successful '
Self.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 </a>) ', ' <input name= ' msg ')
CRLF = ' n '
If _msg_tmp.find (' rn ')!=-1:crlf = ' RN '
_msg = Re.findall (R ' (. +) <br/> ' + CRLF + ' (. +) <br/> ', _msg_tmp)
For _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, ' GBK '). 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 ' Send Message to ', QQ, ' success '
#收到消息的接口, overload or override this method
def getmsg (self,data):
print data[' time ', "received", data[' Nick '], " (", data[' QQ ']," "New Message"
Self.sendmsgfun (data[' QQ '],data[' Nick ']+ ', I have received your message: ' + data[' msg '])
QQ = PYQQ ()
Qq.login ()
|