1、接入校验,这里我用的是Python3,在执行 pip install web.py 安装 web.py 的时候报错,改为 pip install web.py==0.40.dev0 即可正常安装

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

import web

import hashlib

urls = (

'/wx', 'Check',

)

class Check(object):

def GET(self):

try:

data = web.input()

signature = data.signature

timestamp = data.timestamp

nonce = data.nonce

echostr=data.echostr

token = "yufu" #这里要与微信公众平台基本配置中的Token保持一致

list = [token, timestamp, nonce]

# 对token、timestamp和nonce按字典排序

list.sort()

# 将排序后的结果拼接成一个字符串

list = ''.join(list)

# 对结果进行sha1加密

hashcode = hashlib.sha1(list.encode('utf8')).hexdigest()

# 加密结果如果与微信服务器发送过来的签名一致,则校验通过,原样返回echostr,否则返回空

if hashcode == signature:

return echostr

else:

return ''

except Exception:

return Exception

if __name__ == '__main__':

app = web.application(urls, globals())

app.run()

代码完成之后运行

2、填写服务器配置,URL填入域名加“/wx”,Token要与代码中的token一致,点击提交。

在这之前我提交多次,老是显示连接URL超时,后来发觉问题出在sha1加密。原先我加密的代码是 hashcode = hashlib.sha1(list).hexdigest(),没有指定要加密的字符串的字符编码,改为 hashcode = hashlib.sha1(list.encode('utf8')).hexdigest() 即可。

更多推荐

python接入微信平台超时_Python搭建微信公众平台之接入指南