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

本文详细介绍如何利用web.py框架和必要的Python库如libxml2, libxslt, lxmlpython来搭建微信公众号的服务器端接口。通过具体代码示例,包括创建必要的文件夹和文件,设置URL映射,以及实现GET请求的验证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

满足开发前提:

	已安装web.py
	已安装libxml2, libxslt, lxml python
  1. 在服务器上创建一个文件夹比如wx
  2. 在wx文件夹中创建两个文件main.pyhandle.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比特本特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值