微信 模拟服务器,使用PYTHON 模拟微信服务端

# -*- coding: utf-8 -*-

#/usr/bin/env python

__version__ = 0.1

__author__ = http://weibo.com/wtmmac

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

# 配置

interface_url = www.xxx.net

interface_path = /interface/weixin.php

Token = weixin

messages = {

subscribe :

123456789

,

# 用户发送文本信息

text:

1348831860

1234567890123456

}

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()

# -*- coding: utf-8 -*- #/usr/bin/env python __version__ = 0.1 __author__ = http://weibo.com/wtmmac import sys, urllib, httplib, time, hashlib, random # 配置 interface_url = www.xxx.net interface_path = /interface/weixin.php Token = weixin messages = { subscribe : 123456789 , # 用户发送文本信息 text: 1348831860 1234567890123456 } 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()
经验分享 程序员 微信小程序 职场和发展