鱼C论坛

 找回密码
 立即注册
查看: 2247|回复: 1

django接入微信公众号提示url超时

[复制链接]
发表于 2017-7-8 11:34:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我用pythonanywhere假设了个django准备做微信公众号的后台,可是在接入的过程中一直报错,我百度了好几种写法,都报错,我很费解是什么原因。希望大神看下。

视图代码如下:
    #coding:utf-8
    import hashlib
    import json
    import time
    import urllib.request
    from django.shortcuts import render
    from django.utils.encoding import smart_str
    from django.views.decorators.csrf import csrf_exempt
    from django.http import HttpResponse
    from xml.etree import ElementTree
    WEIXIN_TOKEN = 'token'
    @csrf_exempt
    def Wechat(request):
        if request.method == "GET":
            signature = request.GET.get("signature", None)
            timestamp = request.GET.get("timestamp", None)
            nonce = request.GET.get("nonce", None)
            echostr = request.GET.get("echostr", None)
            if signature != None:
                token = WEIXIN_TOKEN
                tmp_list = [token, timestamp, nonce]
                tmp_list.sort()
                tmp_str = "%s%s%s" % tuple(tmp_list)
                tmp_str = hashlib.sha1(tmp_str.encode("utf-8")).hexdigest()
                if tmp_str == signature:
                    return HttpResponse(echostr)
            else:
                return HttpResponse("Error")
        else:
            request_xml = ElementTree.fromstring(request.body)
            fromUser = request_xml.find('ToUserName').text
            toUser = request_xml.find('FromUserName').text
            msgtype = request_xml.find('MsgType').text
            nowtime = str(int(time.time()))
            if msgtype == 'text':
                content = request_xml.find('Content').text
                if content == '帮助':
                    message = 'subscribe'
                elif content == '【收到不支持的消息类型,暂无法显示】':
                    message = '收到动态表情'
                else:
                    message = '收到文本消息'
            elif msgtype == 'location':
                label = request_xml.find('Label').text
                message = (u'收到位置信息:') + label
            elif msgtype == 'image':
                message = (u'收到图片')
            elif msgtype == 'voice':
                message = (u'收到语音')
            elif msgtype == 'shortvideo':
                message = (u'收到短视频')
            elif msgtype == 'link':
                message = (u'收到链接')
            elif msgtype == 'event':
                event = request_xml.find('Event').text
                if event == 'subscribe':
                    message = 'subscribe'
            return render(request,'text.xml',{'toUser': toUser, 'fromUser': fromUser, 'nowtime': nowtime, 'content': message})
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-8 13:49:42 | 显示全部楼层
我自己研究下了,解决了。
原因有两个,
1,pythonanywhere每个更改文件都要relode,我没有做到。
2,hash加密的写法有问题。


这个主题怎么关,我不会啊,麻烦管理员关闭下这个帖子,谢谢啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 13:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表