利用webqq协议使用python登录qq发消息源码参考

开源的,地址在https://github.com/atupal/webqq

等有时间了写一个完整的cli qq客户端。
使用方法:python login_qq.py即可


代码如下:

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, 99999999))
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*7e8-&u1=http%3A%2F%2Fweb2.qq.com%2Floginproxy.html&r=0.8210972726810724'
req = urllib2.Request(url)
#self.mycookie += "confirmuin=" + self.user + ";"
#req.add_header('Cookie', 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&0.45644426648505' + str(random.randint(10,99))
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 + '&webqq_type=10&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%2Fweb.qq.com%2Floginproxy.html%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?target=self&style=5&mibao_css=m_webqq&appid=1003903&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fweb.qq.com%2Floginproxy.html&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%22status%22%3A%22online%22%2C%22ptwebqq%22%3A%22' + self.ptwebqq + '%22%2C%22passwd_sig%22%3A%22%22%2C%22clientid%22%3A%22'+self.clientid+'%22%2C%22psessionid%22%3Anull%7D&clientid='+self.clientid+'&psessionid=null'
req = urllib2.Request(url, data)
#req.add_header('Cookie', 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%22vfwebqq%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%22vfwebqq%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%22clientid%22%3A%22'+self.clientid+'%22%2C%22psessionid%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('Cookie', 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%22face%22%3A237%2C%22content'+urllib.quote(r'":"[\"'+msg+r'\",\"\\n【提示:此用户正在使用shift webQq】\",[\"font\",{\"name\":\"宋体\",\"size\":\"10\",\"style\":[0,0,0],\"color\":\"000000\"}]]","')+'msg_id%22%3A13190001%2C%22clientid%22%3A%22'+self.clientid+'%22%2C%22psessionid%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('Cookie', 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%22face%22%3A237%2C%22content'+urllib.quote(r'":"[\"'+msg+r'\",\"\\n【提示:此用户正在使用shift webQq】\",[\"font\",{\"name\":\"宋体\",\"size\":\"10\",\"style\":[0,0,0],\"color\":\"000000\"}]]","')+'msg_id%22%3A13190001%2C%22clientid%22%3A%22'+self.clientid+'%22%2C%22psessionid%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('2236071402', 'geisf')
if __name__ == "__main__":
main()

(0)

相关推荐

  • QQ多帐号自动登录批处理脚本

    复制代码 代码如下: '定义QQ程序路径.帐名.密码 Dim Program1,a,b Program1= "C:\Program Files\Tencent\QQ\Bin\QQ.exe" Set WshShell=createobject("wscript.shell") '运行QQ主程序 Set oExec=WshShell.Exec(Program1) WScript.Sleep 1000 '激活QQ窗口 WshShell.AppActivate "

  • python登录QQ邮箱发信的实现代码

    复制代码 代码如下: # -*- coding: cp936 -*-from email.Header import Headerfrom email.MIMEText import MIMETextfrom email.MIMEMultipart import MIMEMultipartimport smtplib, datetime#创建一个带附件的实例msg = MIMEMultipart()#构造附件att = MIMEText(open('f:\\文件名.doc', 'rb').rea

  • jquery仿QQ登录账号选择下拉框效果

    QQ登录的时候,可以选择以前登录过的账户,这个东西也可以在网站登录的时候用到,所以我就想做一个这样的插件:在网上查了很多,没有找到合适自己的,所以决定自动制作一个. 原理就是一个textbox加一个ul模拟下拉框,用font模拟一个下拉按钮. 一.制作静态效果 先用css和html,做出一个应该有的样子.这里这两个我使用的是字体,可以在icomoon网站上面自己制作.用字体的好处是和输入框定位很方便,而且还可以控制大小颜色等,唯一的不足是IE6和IE7由于不支持:before选择器,导致无法显示

  • js如何调用qq互联api实现第三方登录

    qq互联js SDK:http://wiki.connect.qq.com/js_sdk%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E 复制代码 代码如下: <script type=text/javascript> 在页面header中引入:<script type="text/javascript" src="http://qzonestyle.gtimg.cn/qzone/openapi/qc_loader.js"

  • Python3.2模拟实现webqq登录

    这是我最初学习时自己做的一个python模拟登录webqq的实例代码,具体代码如下 import hashlib from urllib import request,parse from http import cookiejar import re,random,time import threading as th import json.encoder as json_encode import json.decoder as json_decode class QQ: ""&

  • 微信QQ的二维码登录原理js代码解析

    在很多地方就是都出现了使用二维码登录,二维码付款,二维码账户等应用(这里的二维码种马,诈骗就不说了),二维码验证,多终端辅助授权应用开始多起来,这里先说下啥是二维码,其实二维码就是存了二进制数据的黑白图片,当出现要求二维码登录的时候,服务器会生成一条临时的唯一的二维码信息,发送到客户端以二维码(图片)的形式写入到网页,然后你就会看到统一的四个方形的二维码,如果做的好这个二维码信息应该是有时效的,这里暂且不考虑这些,就简单的微信登录作为例子看看吧: 首先说下整个授权流程: 在客户端网页中会不断向服

  • PHP实现QQ快速登录的方法

    前言: PHP实现QQ快速登录,罗列了三种方法 方法一:面向过程,回调地址和首次触发登录写到了一个方法页面[因为有了if做判断], 方法二,三:面向对象 1.先调用登录方法,向腾讯发送请求, 2.腾讯携带本网站唯一对应参数OPENID,ACCESSTOKEN,返回到对应回调页面, 3.回调页面接受到腾讯的参数后,通过这个两个参数,再发出对应的请求,如查询用户的数据. 4.腾讯做出对应的操作,如返回这个用户的数据给你 即使你没看懂,也没关系,按照我下面的流程来,保证你可以实现. 前期准备: 使用人

  • QQ登录 PHP OAuth示例代码

    根据官方文档编写 复制代码 代码如下: <?php /** * 申请http://connect.opensns.qq.com/apply * 列表http://connect.opensns.qq.com/my */ session_start(); $qq_oauth_config = array( 'oauth_consumer_key'=>'*******',//APP ID 'oauth_consumer_secret'=>'******************',//APP

  • Android QQ登录界面绘制代码

    先看看效果图: 首先过程中碰到的几个问题: 1.对 EditText 进行自定义背景 2.运行时自动 EditText 自动获得焦点 3.在获得焦点时即清空 hint ,而不是输入后清空 4.清空按钮的出现时机(在得到焦点并且有输入内容时) ---  这些问题都有一一解决 --- 以下是代码: 布局 fragment_main(问题2) <!-- android:focusable="true" android:focusableInTouchMode="true&qu

  • Android调用第三方QQ登录代码分享

    本文为大家分享了调用QQ登录的相关代码,希望对大家有帮助,减少项目开发的时间,具体内容如下 1.去QQ开放平台注册帐号(http://open.qq.com/),为应用申请QQ的APP_ID , 并下载相关的jar包,放到工程的lib目录下. 2.在Manifest.xml里注册QQ相关的Activity,代码如下 <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOr

随机推荐