微信服务端模拟,信服务端模拟,最近开发微信接口,配合开
微信服务端模拟,信服务端模拟,最近开发微信接口,配合开
最近开发微信接口,配合开发弄了这个微信服务端的模拟以方便调试。
# -*- coding: utf-8 -*-#/usr/bin/env python'''微信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 hashdef 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()#该片段来自于http://byrx.net
相关内容
- 删除列表中重复的元素,删除列表重复元素,lists=[20,1
- python中随机数的查找,python随机数查找,1. random.ra
- wxpython与logging模块结合显示实时日志,wxpythonlogging,#!
- open phone's app,openphoneapp,def open_app
- 装饰器 decorator,装饰decorator,装饰器的简单应用方法A和
- wxpython tab标签式 gui实现方法,wxpythongui,import wxcla
- quick sort,quick,class QuickS
- python实现的简单LR(1)状态表,pythonlr,只是四则混合运算
- 检查resin的gc相关log的脚本(nagios插件版),resinnagios,输入
- python里用java的模块SmartXLS和jpype修改excel文件,smartxls
评论关闭