微信公众平台-服务器端-Python3.x代码提供

满足开发前提:

	已安装web.py
	已安装libxml2, libxslt, lxml python
  1. 在服务器上创建一个文件夹比如wx
  2. 在wx文件夹中创建两个文件main.py,handle.py 内容分别如下
  3. python mian.py 80 启动服务
# -*- coding: utf-8 -*-
# filename: main.py
import web
from handle import Handle
urls = (
    '/wx', 'Handle',
)
if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()
# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web


class Handle(object):
    def GET(self):
        try:
            data = web.input()
            if len(data) == 0:
                return "hello, this is handle view"
            signature = data.signature
            timestamp = data.timestamp
            nonce = data.nonce
            echostr = data.echostr

            token = "abc123"  # 请按照公众平台官网\基本配置中信息填写
            list = [token, timestamp, nonce]
            list.sort()
            print(data)
            list = [token, timestamp, nonce]
            list.sort()
            # python2.7
            # sha1 = hashlib.sha1()
            # map(sha1.update, list)
            # hashcode = sha1.hexdigest()

            # python3.x
            sha1 = hashlib.sha1()
            sha1.update(list[0].encode("utf-8"))
            sha1.update(list[1].encode("utf-8"))
            sha1.update(list[2].encode("utf-8"))
            hashcode = sha1.hexdigest()  # 获取加密串

            # python3.x
            # temp = ''.join(list)
            # sha1 = hashlib.sha1(temp.encode('utf-8'))
            # map(sha1.update, list)
            # hashcode = sha1.hexdigest()
            # print("handle/GET func: hashcode, signature: ", hashcode, signature)
            if hashcode == signature:
                return echostr
            else:
                return ''
        except Exception:
            return Exception

你可能感兴趣的:(微信)