最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中调用wechat_sdk出错:AttributeError function object has no attribute check_signature

Flask crifan 3447浏览 0评论

折腾:

[已解决]尝试把Python版微信SDK wechat-python-sdk集成到Flask中

期间,对于代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-  
from flask import Flask, g, request, make_response
from wechat_sdk import WechatConf
from wechat_sdk import WechatBasic
app = Flask(__name__)
# app.debug=True
wechatConf = WechatConf(
    token=’sipevents’, 
    appid=’wx9xxxxxxxxxxxxxxd’,
    appsecret=’8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxd’,
    # 可选项:normal/compatible/safe,分别对应于 明文/兼容/安全 模式
    encrypt_mode=’safe’,
    # 如果传入此值则必须保证同时传入 token, appid
    encoding_aes_key=’bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt’
)
wechat = WechatBasic(conf=wechatConf)
@app.route(“/”)
def hello():
    return “Hello SIPEvents!”
@app.route(‘/wechat_auth’, methods=[‘GET’, ‘POST’])
def wechat():
    signature = request.args.get(‘signature’, ”)
    timestamp = request.args.get(‘timestamp’, ”)
    nonce = request.args.get(‘nonce’, ”)
    echostr = request.args.get(‘echostr’, ”)
    encrypt_type = request.args.get(‘encrypt_type’, ‘safe’)
    msg_signature = request.args.get(‘msg_signature’, ”)
    app.logger.debug(‘signature=%s, timestamp=%s, nonce=%s, echostr=%s, encrypt_type=%s, msg_signature=%s’, signature, timestamp, nonce, echostr, encrypt_type, msg_signature)
    if wechat.check_signature(signature, timestamp, nonce):
        app.logger.debug(“wechat check_signature OK”)
        if request.method == ‘GET’ :
            return make_response(echostr)
        else :
            return make_response(“current not support wechat auth POST”)
    else :
        app.logger.debug(“wechat check_signature wrong”)
        return make_response(“wechat auth failed”)
if __name__ == ‘__main__’:
    app.run(debug=True)

运行,去验证,结果出现:

  File “/usr/share/nginx/html/SIPEvents/wechat_auth.py”, line 36, in wechat
    if wechat.check_signature(signature, timestamp, nonce):
AttributeError: ‘function’ object has no attribute ‘check_signature’

很是诡异:

此处调用的wechat_sdk,即可可以通过编译

说明是找到了wechat_sdk,并import对应的类的

而wechat_sdk中,肯定也是有

check_signature

-》官网文档:

2. 官方接口 – wechat-python-sdk

和:

github的源码

wechat-python-sdk/basic.py at master · doraemonext/wechat-python-sdk

都可以找到的。

Flask AttributeError: ‘function’ object has no attribute

AttributeError: ‘function’ object has no attribute ‘name’ · Issue #1327 · pallets/flask

说是重名了

-》抽空自己重命名,试试?

python 2.7 – Google App Engine + Flask + Stripe: Attribute Error: AttributeError: ‘function’ object has no attribute ‘Customer’ – Stack Overflow

python – Registering route on blueprint raises AttributeError: ‘function’ object has no attribute ‘route’ – Stack Overflow

python – Flask Blueprint AttributeError: ‘module’ object has no attribute ‘name’ error – Stack Overflow

python – Structuring Flask using Blueprints error – Stack Overflow

python – flask-mail AttributeError: ‘function’ object has no attribute ‘send’ – Stack Overflow

难道是:

此处的wechat和之前的,内置的?别的地方的?我所不知道的,wechat,冲突了?重名了?

那去换个名字试试

从:

wechat = WechatBasic(conf=wechatConf)
@app.route(‘/wechat_auth’, methods=[‘GET’, ‘POST’])
def wechat():
    if wechat.check_signature(signature, timestamp, nonce):

改为:

wechat = WechatBasic(conf=wechatConf)
@app.route(‘/wechat_auth’, methods=[‘GET’, ‘POST’])
def wechat_auth():
    if wechat.check_signature(signature, timestamp, nonce):

[总结]

此处的提示是:

AttributeError: ‘function’ object has no attribute ‘check_signature’

指的是:

此处的这句代码:

    if wechat.check_signature(signature, timestamp, nonce):

中的wechat,是个function函数

这个类型为function的wechat,没有叫做check_signature的属性

但实际上,我们代码的本意是:

此处的wechat是个类WechatBasic的实例

-》而不是function函数

之所以此处这么提示,是因为:

在这句出错的代码之前,有一句:

def wechat():

从而把之前最早的,正确初始化了的WechatBasic的实例wechat

重新定义为function了

所以此处才报错的。

而把此处的函数改名为:

def wechat_auth():

即可解决重名问题,此时

    if wechat.check_signature(signature, timestamp, nonce):

中的wechat,就保持了,正常的,最开始初始化了的WechatBasic的实例了

[举一反三]

以后再在Python中遇到类似的:

看起来明明是有对应属性,但是却报错:

AttributeError: ‘function’ object has no attribute ’some_property’

则意味着:

此处的some_property的所属变量,类似于:

errorValue.some_property

中的errorValue

和前面某个地方的,有个同名的函数

-》

解决办法也很简答:

要么是重新命名有冲突的函数errorValue

要么是重新命名,原先的所使用的变量errorValue

就可以了。

转载请注明:在路上 » [已解决]Flask中调用wechat_sdk出错:AttributeError function object has no attribute check_signature

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.310 seconds, using 22.19MB memory