利用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 相关文章推荐
JS执行删除前的判断代码
Feb 18 Javascript
js调试系列 源码定位与调试[基础篇]
Jun 18 Javascript
js使用for循环与innerHTML获取选中tr下td值
Sep 26 Javascript
jquery实现Ctrl+Enter提交表单的方法
Jul 21 Javascript
整理JavaScript创建对象的八种方法
Nov 03 Javascript
常用原生JS兼容性写法汇总
Apr 27 Javascript
移动端点击图片放大特效PhotoSwipe.js插件实现
Aug 25 Javascript
详解React 16 中的异常处理
Jul 28 Javascript
vue轮播图插件vue-awesome-swiper
Nov 27 Javascript
详解Vue之父子组件传值
Apr 01 Javascript
Node.js实现批量下载图片简单操作示例
Jan 18 Javascript
JavaScript如何优化逻辑判断代码详解
Jun 08 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
PHP实现MVC开发得最简单的方法――模型
2007/04/10 PHP
ThinkPHP模版中导入CSS和JS文件的方法
2014/11/29 PHP
PHP implode()函数用法讲解
2019/03/08 PHP
jQuery 入门讲解1
2009/04/15 Javascript
自己使用js/jquery写的一个定制对话框控件
2014/05/02 Javascript
JavaScript中的依赖注入详解
2015/03/18 Javascript
利用jquery制作滚动到指定位置触发动画
2016/03/26 Javascript
使用HTML5+Boostrap打造简单的音乐播放器
2016/08/05 Javascript
self.attachevent is not a function的解决方法
2017/04/04 Javascript
angular学习之从零搭建一个angular4.0项目
2017/07/10 Javascript
vue2.0 常用的 UI 库实例讲解
2017/12/12 Javascript
基于Vue实现拖拽功能
2020/07/29 Javascript
JS中call和apply函数用法实例分析
2018/06/20 Javascript
深入浅析JavaScript中的in关键字和for-in循环
2020/04/20 Javascript
前端 javascript 实现文件下载的示例
2020/11/24 Javascript
[05:56]第十六期——新进3大C之小兔基
2014/06/24 DOTA
Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作示例
2018/04/19 Python
Python爬虫的两套解析方法和四种爬虫实现过程
2018/07/20 Python
Python打开文件,将list、numpy数组内容写入txt文件中的方法
2018/10/26 Python
浅谈Python中eval的强大与危害
2019/03/13 Python
python实现tail -f 功能
2020/01/17 Python
python中把元组转换为namedtuple方法
2020/12/09 Python
俄罗斯马克西多姆家居用品网上商店:Максидом
2020/02/06 全球购物
机关门卫岗位职责
2013/12/30 职场文书
主持词开场白
2014/03/17 职场文书
文明之星事迹材料
2014/05/09 职场文书
党员“四风”问题批评与自我批评思想汇报
2014/10/06 职场文书
五年级上册复习计划
2015/01/19 职场文书
导游词之苏州阳澄湖
2019/11/15 职场文书
浅析InnoDB索引结构
2021/04/05 MySQL
JS异步堆栈追踪之为什么await胜过Promise
2021/04/28 Javascript
MongoDB连接数据库并创建数据等使用方法
2021/11/27 MongoDB
《最终幻想14》6.01版本4月5日推出 追加新任务新道具
2022/04/03 其他游戏
Python使用BeautifulSoup4修改网页内容
2022/05/20 Python
windows系统安装配置nginx环境
2022/06/28 Servers
Apache自带的ab压力测试工具的实现
2022/07/23 Servers