利用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 相关文章推荐
解决html按钮切换绑定不同函数后点击时执行多次函数问题
May 14 Javascript
JavaScript监听和禁用浏览器回车事件实例
Jan 31 Javascript
AngularJS基础学习笔记之简单介绍
May 10 Javascript
深入浅析javascript中的作用域(推荐)
Jul 19 Javascript
微信小程序 免费SSL证书https、TLS版本问题的解决办法
Dec 14 Javascript
详解本地Node.js服务器作为api服务器的解决办法
Feb 28 Javascript
js es6系列教程 - 新的类语法实战选项卡(详解)
Sep 02 Javascript
seajs中模块依赖的加载处理实例分析
Oct 10 Javascript
了解JavaScript中的选择器
May 24 Javascript
node获取客户端ip功能简单示例
Aug 24 Javascript
JS指定音频audio在某个时间点进行播放
Nov 28 Javascript
详解Vue的sync修饰符
May 15 Vue.js
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内存管理之谁动了我的内存
2013/06/20 PHP
腾讯QQ微博API接口获取微博内容
2013/10/30 PHP
PHP添加Xdebug扩展的方法
2014/02/12 PHP
php返回字符串中所有单词的方法
2015/03/09 PHP
php统计数组元素个数的方法
2015/07/02 PHP
PHP递归创建多级目录
2015/11/05 PHP
实现PHP搜索加分页
2016/10/12 PHP
PHP输出图像imagegif、imagejpeg与imagepng函数用法分析
2016/11/14 PHP
JavaScript网页制作特殊效果用随机数
2007/05/22 Javascript
一个报数游戏js版(约瑟夫环问题)
2010/08/05 Javascript
使用jquery局部刷新(jquery.load)从数据库取出数据
2014/01/22 Javascript
JavaScript实现控制打开文件另存为对话框的方法
2015/04/17 Javascript
javascript每日必学之封装
2016/02/23 Javascript
JS中改变this指向的方法(call和apply、bind)
2016/03/26 Javascript
bootstrap-datetimepicker实现只显示到日期的方法
2016/11/25 Javascript
零基础轻松学JavaScript闭包
2016/12/30 Javascript
JavaScript 总结几个提高性能知识点(推荐)
2017/02/20 Javascript
使用vue实现grid-layout功能实例代码
2018/01/05 Javascript
jquery 动态遍历select 赋值的实例
2018/09/12 jQuery
Bootstarp在pycharm中的安装及简单的使用方法
2019/04/19 Javascript
微信小程序实现页面监听自定义组件的触发事件
2020/11/01 Javascript
webpack4从0搭建组件库的实现
2020/11/29 Javascript
python实现同时给多个变量赋值的方法
2015/04/30 Python
python实现求两个字符串的最长公共子串方法
2018/07/20 Python
Python之时间和日期使用小结
2019/02/14 Python
利用django+wechat-python-sdk 创建微信服务器接入的方法
2019/02/20 Python
详解python-图像处理(映射变换)
2019/03/22 Python
Python列表去重复项的N种方法(实例代码)
2020/05/12 Python
python中if及if-else如何使用
2020/06/02 Python
Spartoo英国:欧洲最大的网上鞋店
2016/09/13 全球购物
初中政治教学反思
2014/01/17 职场文书
小学教师培训感言
2014/02/11 职场文书
有关环保的标语
2014/06/13 职场文书
县委务虚会发言材料
2014/10/20 职场文书
Mysql数据库索引面试题(程序员基础技能)
2021/05/31 MySQL
利用For循环遍历Python字典的三种方法实例
2022/03/25 Python