最近开发微信接口,配合开发弄了这个微信服务端的模拟以方便调试。

 
  
  1. # -*- coding: utf-8 -*- 
  2. #/usr/bin/env python 
  3.  
  4. __version__ = '0.1' 
  5. __author__  = 'http://weibo.com/wtmmac' 
  6.  
  7. ''''' 
  8. 微信Server模拟 
  9. ''' 
  10.  
  11. import sys, urllib, httplib, time, hashlib, random 
  12.  
  13. # 配置 
  14. interface_url = 'www.xxx.net' 
  15. interface_path = '/interface/weixin.php' 
  16. Token = 'weixin' 
  17.  
  18. messages = { 
  19.     # 用户关注消息 
  20.     'subscribe' : '''''<xml><ToUserName><![CDATA[测试服务帐号]]></ToUserName> 
  21.     <FromUserName><![CDATA[小黑]]></FromUserName> 
  22.     <CreateTime>123456789</CreateTime> 
  23.     <MsgType><![CDATA[event]]></MsgType> 
  24.     <Event><![CDATA[subscribe]]></Event> 
  25.     <EventKey><![CDATA[EVENTKEY]]></EventKey> 
  26.     </xml>'''
  27.  
  28.     # 用户发送文本信息 
  29.     'text''''''<xml> 
  30.     <ToUserName><![CDATA[测试服务帐号]]></ToUserName> 
  31.     <FromUserName><![CDATA[小黑]]></FromUserName>  
  32.     <CreateTime>1348831860</CreateTime> 
  33.     <MsgType><![CDATA[text]]></MsgType> 
  34.     <Content><![CDATA[周杰伦]]></Content> 
  35.     <MsgId>1234567890123456</MsgId> 
  36.     </xml>''' 
  37.  
  38.  
  39. def make_post(action): 
  40.     '''''模拟用户行为产生的消息提交给接口程序''' 
  41.  
  42.     conn = httplib.HTTPConnection(interface_url) 
  43.  
  44.     headers = { "Content-type""text/xml"
  45.                 "Content-Length""%d" % len(messages[action])} 
  46.  
  47.     # 生成签名相关变量 
  48.     timestamp = int(time.time()) 
  49.  
  50.     nonce = random.randint(1,100000
  51.  
  52.     signature = makeSignature(Token, timestamp, nonce) 
  53.  
  54.     params = urllib.urlencode({'signature': signature, 'timestamp': timestamp, 'nonce': nonce}) 
  55.  
  56.     conn.request("POST", interface_path + "?" +params, "", headers) 
  57.  
  58.     conn.send(messages[action]) 
  59.  
  60.     response = conn.getresponse() 
  61.  
  62.     print response.status, response.reason 
  63.       
  64.     print response.read() 
  65.  
  66.     conn.close() 
  67.  
  68.      
  69.      
  70. def makeSignature(Token, timestamp, nonce): 
  71.     '''''生成签名''' 
  72.     try
  73.         Token = int(Token) 
  74.     except Exception, e: 
  75.         pass 
  76.  
  77.     sorted_arr = map(str, sorted([Token, timestamp, nonce])) 
  78.  
  79.     sha1obj = hashlib.sha1() 
  80.     sha1obj.update(''.join(sorted_arr)) 
  81.     hash = sha1obj.hexdigest() 
  82.  
  83.     return hash 
  84.  
  85. def listAction(): 
  86.     print("======Supported actions:======"
  87.     for i in messages.keys(): 
  88.         print(i) 
  89.     print("=============================="
  90.  
  91. if __name__ == '__main__'
  92.     if len(sys.argv) < 2:    
  93.         print (u"Please input your action"
  94.         listAction() 
  95.     else
  96.         if (messages.has_key(sys.argv[1])): 
  97.             make_post(sys.argv[1]) 
  98.         else
  99.             print("No this action"
  100.             listAction()