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


Posted in Javascript onApril 08, 2013

开源的,地址在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()
Javascript 相关文章推荐
多个iframe自动调整大小的问题
Sep 18 Javascript
JQuery中form验证出错信息的查看方法
Oct 08 Javascript
js 定时器setTimeout无法调用局部变量的解决办法
Nov 28 Javascript
JS实现一个列表中包含上移下移删除等功能
Sep 24 Javascript
JS+CSS实现简易的滑动门效果代码
Sep 24 Javascript
详解有关easyUI的拖动操作中droppable,draggable用法例子
Jun 03 Javascript
javascript ES6 新增了let命令使用介绍
Jul 07 Javascript
解决Jstree 选中父节点时被禁用的子节点也会选中的问题
Dec 27 Javascript
微信小程序之swiper轮播图中的图片自适应高度的方法
Apr 23 Javascript
axios+Vue实现上传文件显示进度功能
Apr 14 Javascript
JavaScript函数式编程(Functional Programming)声明式与命令式实例分析
May 21 Javascript
js实现秒表计时器
Dec 16 Javascript
setInterval,setTimeout与jquery混用的问题
Apr 08 #Javascript
JQueryEasyUI Layout布局框架的使用
Apr 08 #Javascript
jquery 实现二级/三级/多级联动菜单的思路及代码
Apr 08 #Javascript
JQueryEasyUI datagrid框架的基本使用
Apr 08 #Javascript
Jquery中使用setInterval和setTimeout的方法
Apr 08 #Javascript
JQueryEasyUI datagrid框架的进阶使用
Apr 08 #Javascript
基于datagrid框架的查询
Apr 08 #Javascript
You might like
mysql5的sql文件导入到mysql4的方法
2008/10/19 PHP
apache+codeigniter 通过.htcaccess做动态二级域名解析
2012/07/01 PHP
PHP实现取得HTTP请求的原文
2014/08/18 PHP
使用PHP如何实现高效安全的ftp服务器(二)
2015/12/30 PHP
PHP验证码生成原理和实现
2016/01/24 PHP
Laravel给生产环境添加监听事件(SQL日志监听)
2017/06/19 PHP
PHP PDOStatement::getAttribute讲解
2019/02/01 PHP
javascript String 对象
2008/04/25 Javascript
js 提交和设置表单的值
2008/12/19 Javascript
javascript 伪数组实现方法
2010/10/11 Javascript
20款效果非常棒的 jQuery 插件小结分享
2011/11/18 Javascript
Javascript面向对象扩展库代码分享
2012/03/27 Javascript
js 中的switch表达式使用示例
2020/06/03 Javascript
jquery统计输入文字的个数并对其进行判断
2014/01/07 Javascript
Jquery遍历节点的方法小集
2014/01/22 Javascript
jquery中get,post和ajax方法的使用小结
2014/02/04 Javascript
JavaScript中的replace()方法使用详解
2015/06/06 Javascript
详解javascript new的运行机制
2016/01/26 Javascript
学习Angularjs分页指令
2016/07/01 Javascript
Bootstrap3使用typeahead插件实现自动补全功能
2016/07/07 Javascript
ES6函数实现排它两种写法解析
2020/05/13 Javascript
python进程管理工具supervisor的安装与使用教程
2017/09/05 Python
在python中使用requests 模拟浏览器发送请求数据的方法
2018/12/26 Python
Python中asyncio模块的深入讲解
2019/06/10 Python
python列表每个元素同增同减和列表元素去空格的实例
2019/07/20 Python
face++与python实现人脸识别签到(考勤)功能
2019/08/28 Python
浅谈Python的方法解析顺序(MRO)
2020/03/05 Python
python re.match()用法相关示例
2021/01/27 Python
20行代码教你用python给证件照换底色的方法示例
2021/02/05 Python
全面总结使用CSS实现水平垂直居中效果的方法
2016/03/10 HTML / CSS
Sephora丝芙兰马来西亚官方网站:国际化妆品购物
2018/03/15 全球购物
什么情况下你必须要把一个类定义为abstract的
2013/01/06 面试题
申报职称专业技术个人的自我评价
2013/12/12 职场文书
协议书与合同的区别
2014/04/18 职场文书
安全保证书格式
2015/02/28 职场文书
MySQL 十大常用字符串函数详解
2021/06/30 MySQL