应用PYTHON 仿真模拟手机微信服务端

 您如今的部位:  >> 手机微信开发设计 >> 开发设计技术性新闻资讯 开发设计技术性新闻资讯
应用PYTHON 仿真模拟手机微信服务端 海口市保持中立高新科技 | :40 | 阅读文章:7387
#-*-coding:utf-8-*-#/usr/bin/envpython__version__='0.1'__author__='wtmmac''''手机微信Server仿真模拟'''importsys,urllib,httplib,time,hashlib,random#配备interface_url=''interface_path='/'Token='weixin'messages={#客户关心信息'subscribe':''' xml ToUserName ![CDATA[检测服务账号]] /ToUserName FromUserName ![CDATA[小黑]] /FromUserName CreateTime /CreateTime MsgType ![CDATA[event]] /MsgType Event ![CDATA[subscribe]] /Event EventKey ![CDATA[EVENTKEY]] /EventKey /xml ''',#客户推送文字信息内容'text':''' xml ToUserName ![CDATA[检测服务账号]] /ToUserName FromUserName ![CDATA[小黑]] /FromUserName CreateTime /CreateTime MsgType ![CDATA[text]] /MsgType Content ![CDATA[周董]] /Content MsgId 23456 /MsgId /xml '''}defmake_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()printresponse.status,response.reasonprintresponse.read()conn.close()defmakeSignature(Token,timestamp,nonce):'''转化成签字'''try:Token=int(Token)exceptException,e:passsorted_arr=map(str,sorted([Token,timestamp,nonce]))sha1obj=hashlib.sha1()sha1obj.update(''.join(sorted_arr))hash=sha1obj.hexdigest()returnhashdeflistAction():print("======Supportedactions:======")foriinmessages.keys():print(i)print("==============================")if__name__=='__main__':iflen(sys.argv) 2:print(u"Pleaseinputyouraction")listAction()else:if(messages.has_key(sys.argv[1])):make_post(sys.argv[1])else:print("Nothisaction")listAction()