Use webqq protocol to use python to log on to qq and send the source code reference

Source: Internet
Author: User
Tags sendmsg

Open source, address in https://github.com/atupal/webqq

Wait for time to write a complete cli qq client.
Usage: python login_qq.py

Copy codeThe Code is as follows: import urllib2
Import httplib2
Import re
Import random
From encryption import QQmd5
Import cookielib
Import requests
Import getpass
Import time
Import json
Import urllib
Class webqq:
Def _ init _ (self, user, pwd ):
Self. cookies = cookielib. CookieJar ()
Self. opener = urllib2.build _ opener (
Urllib2.HTTPHandler (),
Urllib2.HTTPSHandler (),
Urllib2.HTTPCookieProcessor (self. cookies ),
)
Urllib2.install _ opener (self. opener)
Self. user = user
Self. pwd = pwd
Self. mycookie = ";"
# Self. clientid = "21485768"
# Self. clientid = "34592990"
Self. clientid = str (random. randint (10000000,999 99999 ))
Def getSafeCode (self ):
Url = 'https: // ssl.ptlogin2.qq.com/check? Uin = '+ str (self. user) +' & appid = 1003903 & js_ver = 10017 & js_type = 0 & login_sig = 0ihp3t5ghfoonssle-98x9hy4uaqmpvu * 8 * odgl5vyerelcb8fk-y3ts6c3 * found & r = 100'
Req = urllib2.Request (url)
# Self. mycookie + = "confirmuin =" + self. user + ";"
# Req. add_header ('cooker', self. mycookie)
Req = urllib2.urlopen (req)
# Cs = ['% s = % s' % (c. name, c. value) for c in self. cookies]
# Self. mycookie + = ";". join (cs)
Verifycode = re. search (r "'(\ d)', '(. +)', '(. +)'", req. read ())
Self. check = verifycode. group (1)
Self. verifycode1 = verifycode. group (2)
Self. verifycode2 = verifycode. group (3)
If self. check = "1 ":
Url = 'https: // ssl.captcha.qq.com/getimage? & Uin = '+ str (self. user) +' & aid = 1002101 & 000000' + str (random. randint (0.45644426648505 ))
Req = urllib2.Request (url)
Req = urllib2.urlopen (req)
Self. fi = open ("./image. jgp", "wb ")
While 1:
C = req. read ()
If not c:
Break
Else: self. fi. write (c)
Self. fi. close ()
Self. verifycode1 = raw_input ("verifer :")
Print self. check, self. verifycode1, self. verifycode2
Def loginGet (self ):
# Cs = ['% s = % s' % (c. name, c. value) for c in self. cookies]
# Self. mycookie + = ";" ";". join (cs)
Login_url = 'https: // ssl.ptlogin2.qq.com/login? U = '+ self. user + '& p =' + str (QQmd5 (). md5_2 (self. pwd, self. verifycode1, self. verifycode2) + '& verifycode =' + self. verifycode1 + quota % 3Flogin2qq % 3D1% 26webqq_type % 3D10 & h = 1 & ptredirect = 0 & ptlang = 2052 & from_ui = 1 & pttype = 1 & dumy = & fp = loginerroralert & action = 2-14-32487 & mibao_css = m_webqq & t = 1 & g = 1 & js_type = 0 & js_ver = 10015 & login_sig = 0ihp3t5ghfoonssle-98x9hy4uaqmpvu * 8 * odgl5vyerelcb8fk-y3ts6c3 * 7e8 -'
Req = urllib2.Request (login_url)
Req. add_header ("Referer", "https://ui.ptlogin2.qq.com/cgi-bin/login? Logs & f_url = loginerroralert & strong_login = 1 & login_state = 10 & t = 20121029001 ")
# Req. add_header ("Cookie", self. mycookie)
# Self. opener. addheaders. append ("Cookie", self. mycookie ))
Req = urllib2.urlopen (req)
Print req. read ()
For cookie in self. cookies:
Print cookie. name, ":", cookie. value
If cookie. name = 'ptwebqq ':
Self. ptwebqq = cookie. value
Print urllib2.urlopen ('HTTP: // web2.qq.com/web2/get_msg_tip? Uin = & tp = 1 & id = 0 & retype = 1 & rc = 0 & lv = 3 & t = 1358252543124 '). read ()
# Cs = ['% s = % s' % (c. name, c. value) for c in self. cookies]
# Self. mycookie + = ";" ";". join (cs)
Def loginPost (self ):
Url = 'HTTP: // d.web2.qq.com/channel/login2'
Data = 'r = % 7B % 22 status % 22% 3A % 22 online % 22% 2C % 22 ptwebqq % 22% 3A % 22 '+ self. ptwebqq + '% 22% 2C % 22passwd_sig % 22% 3A % 22% 22% 2C % 22 clientid % 22% 3A % 22' + self. clientid + '% 22% 2C % 22 psessionid % 22% 3 Anull % 7D & clientid =' + self. clientid + '& psessionid = null'
Req = urllib2.Request (url, data)
# Req. add_header ('cooker', self. mycookie)
Req. add_header ('Referer', 'HTTP: // d.web2.qq.com/proxy.html? V = 20110331002 & callback = 1 & id = 2 ')
Req = urllib2.urlopen (req)
Self. result = json. load (req)
Print self. result ['result'] ['vfwebqq'], self. result ['result'] ['psessionid']
Def getGroupList (self ):
Url = 'HTTP: // s.web2.qq.com/api/get_group_name_list_mask2'
Data = 'r = % 7B % 22 vfwebqq % 22% 3A % 22' + self. result ['result'] ['vfwebqq'] + '% 22% 7D'
Req = urllib2.Request (url, data)
Req. add_header ('Referer', 'HTTP: // s.web2.qq.com/proxy.html? V = 20110412001 & callback = 1 & id = 1 ')
Req = urllib2.urlopen (req)
Self. group = json. load (req)
Pass
Def getFriend (self ):
Url = 'HTTP: // s.web2.qq.com/api/get_user_friends2'
Data = 'r = % 7B % 22 vfwebqq % 22% 3A % 22' + self. result ['result'] ['vfwebqq'] + '% 22% 7D'
Req = urllib2.Request (url, data)
Req. add_header ('Referer', 'HTTP: // s.web2.qq.com/proxy.html? V = 20110412001 & callback = 1 & id = 1 ')
Req = urllib2.urlopen (req)
Self. friend = json. load (req)
Print self. friend
Pass
Def getMeg (self ):
Print urllib2.urlopen ('HTTP: // web2.qq.com/web2/get_msg_tip? Uin = & tp = 1 & id = 0 & retype = 1 & rc = 0 & lv = 3 & t = 1358252543124 '). read ()
Pass
Def poll2 (self ):
Url = 'HTTP: // d.web2.qq.com/channel/poll2'
Data = 'r = % 7B % 22 clientid % 22% 3A % 22' + self. clientid + '% 22% 2C % 22 psessionid % 22% 3A % 22' + self. result ['result'] ['psessionid'] + '% 22% 2C % 22key % 22% 3A0% 2C % 22ids % 22% 3A % 5B % 5D % 7D & clientid =' + self. clientid + '& psessionid =' + self. result ['result'] ['psessionid']
Req = urllib2.Request (url, data)
# Req. add_header ('cooker', self. mycookie)
Req. add_header ('Referer', 'HTTP: // d.web2.qq.com/proxy.html? V = 20110331002 & callback = 1 & id = 3 ')
Result = json. load (urllib2.urlopen (req ))
Print result
Def sendMsg (self, uin, msg ):
Url = 'HTTP: // d.web2.qq.com/channel/send_buddy_msg2'
Data = 'r = % 7B % 22to % 22% 3A '+ uin +' % 2C % 22 face % 22% 3A237% 2C % 22content' + urllib. quote (R' ":" [\ "'+ msg + R' \", \ "\ n [prompt: This user is using shift webQq ]\", [\ "font \", {\ "name \": \ "\", \ "size \": \ "10 \", \ "style \": [000000, 0], \ "color \": \ "\"}] "," ') + 'msg _ id % 22% 3A13190001% 2C % 22 clientid % 22% 3A % 22 '+ self. clientid + '% 22% 2C % 22 psessionid % 22% 3A % 22' + self. result ['result'] ['psessionid'] + '% 22% 7D & clientid =' + self. clientid + '& psessionid =' + self. result ['result'] ['psessionid']
Req = urllib2.Request (url, data)
# Req. add_header ('cooker', self. mycookie)
Req. add_header ('Referer', 'HTTP: // d.web2.qq.com/proxy.html? V = 20110331002 & callback = 1 & id = 2 ')
Print urllib2.urlopen (req). read ()
Pass
Def sendQunMsg (self, uin, msg ):
Url = 'HTTP: // d.web2.qq.com/channel/send_qun_msg2'
Data = 'r = % 7B % 22group_uin % 22% 3A '+ uin +' % 2C % 22 face % 22% 3A237% 2C % 22content' + urllib. quote (R' ":" [\ "'+ msg + R' \", \ "\ n [prompt: This user is using shift webQq ]\", [\ "font \", {\ "name \": \ "\", \ "size \": \ "10 \", \ "style \": [000000, 0], \ "color \": \ "\"}] "," ') + 'msg _ id % 22% 3A13190001% 2C % 22 clientid % 22% 3A % 22 '+ self. clientid + '% 22% 2C % 22 psessionid % 22% 3A % 22' + self. result ['result'] ['psessionid'] + '% 22% 7D & clientid =' + self. clientid + '& psessionid =' + self. result ['result'] ['psessionid']
Req = urllib2.Request (url, data)
Req. add_header ('Referer', 'HTTP: // d.web2.qq.com/proxy.html? V = 20110331002 & callback = 1 & id = 2 ')
Print urllib2.urlopen (req). read ()
Pass
Def main ():
User = raw_input ('qq :')
Pwd = getpass. getpass ('password :')
Qq = webqq (user, pwd)
Qq. getSafeCode ()
Qq. loginGet ()
Qq. loginPost ()
Qq. getGroupList ()
Qq. getFriend ()
While 0:
Time. sleep (0.5)
Qq. poll2 ()
For I in range (100 ):
Print 'to', qq. friend ['result'] ['info'] [0] ['uin']
Print 'to', qq. group ['result'] ['gnamelist'] [10]
# Qq. sendMsg (str (qq. friend ['result'] ['info'] [0] ['uin']), 'clientjsfzhiyong ')
MS =''
For _ in xrange (I ):
MS + = '. '
Qq. sendQunMsg (str (qq. group ['result'] ['gnamelist'] [10] ['gid']), MS)
# Qq. sendMsg ('20140901', 'geisf ')
If _ name _ = "_ main __":
Main ()

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.