搜索
系统检测到您的用户名不符合规范:

使用PYTHON 模拟微信服务端

浏览:2421 发布日期:2016年01月05日 分类:技术分享
# -*- coding: utf-8 -*-
#/usr/bin/env python

__version__ = '0.1'
__author__ = 'http://weibo.com/wtmmac'

'''
微信Server模拟
'''

import sys, urllib, httplib, time, hashlib, random

# 配置
interface_url = 'www.xxx.net'
interface_path = '/interface/weixin.php'
Token = 'weixin'

messages = {
# 用户关注消息
'subscribe' : '''<xml><ToUserName><![CDATA[测试服务帐号]]></ToUserName>
<FromUserName><![CDATA[小黑]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
<EventKey><![CDATA[EVENTKEY]]></EventKey>
</xml>''',

# 用户发送文本信息
'text': '''<xml>
<ToUserName><![CDATA[测试服务帐号]]></ToUserName>
<FromUserName><![CDATA[小黑]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[周杰伦]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>'''

}

def make_post(action):
'''模拟用户行为产生的消息提交给接口程序'''

conn = httplib.HTTPConnection(interface_url)

headers = { "Content-type": "text/xml",
"Content-Length": "%d" % len(messages[action])}

# 生成签名相关变量
timestamp = int(time.time())

nonce = random.randint(1,100000)

signature = makeSignature(Token, timestamp, nonce)

params = urllib.urlencode({'signature': signature, 'timestamp': timestamp, 'nonce': nonce})

conn.request("POST", interface_path + "?" +params, "", headers)

conn.send(messages[action])

response = conn.getresponse()

print response.status, response.reason

print response.read()

conn.close()



def makeSignature(Token, timestamp, nonce):
'''生成签名'''
try:
Token = int(Token)
except Exception, e:
pass

sorted_arr = map(str, sorted([Token, timestamp, nonce]))

sha1obj = hashlib.sha1()
sha1obj.update(''.join(sorted_arr))
hash = sha1obj.hexdigest()

return hash

def listAction():
print("======Supported actions:======")
for i in messages.keys():
print(i)
print("==============================")

if __name__ == '__main__':
if len(sys.argv) < 2:
print (u"Please input your action")
listAction()
else:
if (messages.has_key(sys.argv[1])):
make_post(sys.argv[1])
else:
print("No this action")
listAction()
最佳答案
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
wemallwemall
积分:932 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /