利用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 相关文章推荐
JavaScript 基础篇之运算符、语句(二)
Apr 07 Javascript
js字符串转成JSON
Nov 07 Javascript
asm.js使用示例代码
Nov 28 Javascript
JQuery中serialize()、serializeArray()和param()方法示例介绍
Jul 31 Javascript
jQuery晃动层特效实现方法
Mar 09 Javascript
jQuery插件windowScroll实现单屏滚动特效
Jul 14 Javascript
JavaScript基本数据类型及值类型和引用类型
Aug 25 Javascript
javascript中for/in循环及使用技巧
Sep 01 Javascript
jquery实现图片平滑滚动详解
Mar 22 jQuery
JavaScript canvas实现围绕旋转动画
Nov 18 Javascript
AngularJs返回前一页面时刷新一次前面页面的方法
Oct 09 Javascript
Node对CommonJS的模块规范
Nov 06 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
Phpbean路由转发的php代码
2008/01/10 PHP
PHP中如何调用webservice的实例参考
2013/04/25 PHP
php过滤XSS攻击的函数
2013/11/12 PHP
Thinkphp使用mongodb数据库实现多条件查询方法
2014/06/26 PHP
PHP中使用register_shutdown_function函数截获fatal error示例
2015/04/21 PHP
php超快高效率统计大文件行数
2015/07/05 PHP
PHP未登录自动跳转到登录页面
2016/12/21 PHP
Ecshop 后台添加新功能栏目及管理权限设置教程
2017/11/21 PHP
PHP实现非阻塞模式的方法分析
2018/07/26 PHP
php使用goto实现自动重启swoole、reactphp、workerman服务的代码
2020/04/13 PHP
[原创]保存的js无法执行的解决办法
2007/02/25 Javascript
jQuery select控制插件
2009/08/17 Javascript
Fastest way to build an HTML string(拼装html字符串的最快方法)
2011/08/20 Javascript
JS实现遮罩层效果的简单实例
2013/11/12 Javascript
javascript实现iframe框架延时加载的方法
2014/10/30 Javascript
PHP实现的各种中文编码转换类分享
2015/01/23 Javascript
浅析BootStrap模态框的使用(经典)
2016/04/29 Javascript
JS获取屏幕高度的简单实现代码
2016/05/24 Javascript
jQuery+ajax实现滚动到页面底部自动加载图文列表效果(类似图片懒加载)
2016/06/07 Javascript
详解VueRouter进阶之导航钩子和路由元信息
2017/09/13 Javascript
微信、QQ、微博、Safari中使用js唤起App
2018/01/24 Javascript
vue搜索页开发实例代码详解(热门搜索,历史搜索,淘宝接口演示)
2020/04/11 Javascript
Vue实现手机号、验证码登录(60s禁用倒计时)
2020/12/19 Vue.js
深入浅析Python获取对象信息的函数type()、isinstance()、dir()
2018/09/17 Python
pytorch中torch.max和Tensor.view函数用法详解
2020/01/03 Python
python 实现仿微信聊天时间格式化显示的代码
2020/04/17 Python
Django中和时区相关的安全问题详解
2020/10/12 Python
Python基于locals返回作用域字典
2020/10/17 Python
应聘自荐书
2013/10/08 职场文书
体育教师工作总结的自我评价
2013/10/10 职场文书
精彩广告词大全
2014/03/19 职场文书
党委领导班子整改方案
2014/09/30 职场文书
党的群众路线教育实践活动查摆问题自查报告
2014/10/10 职场文书
销售督导岗位职责
2015/04/10 职场文书
个人收入证明格式
2015/06/24 职场文书
MySQL系列之四 SQL语法
2021/07/02 MySQL