wsgi部署微信服务器,验证的GET通过,但是自动回复的POST请求却不行

目标:利用uwsgi和Django试着做个小的微信公众号自动回复(复读机功能), 出现的问题:只处理get请求,不处理post请求

from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import hashlib
from lxml import etree
import xml.etree.ElementTree as ET
import time

# Create your views here.

token = 'chen'

@csrf_exempt
def main(request):
# 如果mian函数被调用,就会打印+++++++++++++++++++++++++++++++++++++ 
                    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    if request.method == "GET":
        #如果是微信的验证GET请求
        if request.GET:
            request_body = request.GET
            signature = request_body.get('signature', None)
            timestamp = request_body.get('timestamp', None)
            nonce = request_body.get('nonce', None)
            echostr = request_body.get('echostr', None)
            hashlist = [token, timestamp, nonce]
            hashlist.sort()
            hashstr = ''.join(hashlist)
            hashcode = hashlib.sha1()
            hashcode.update(hashstr.encode())
            hashcode = hashcode.hexdigest()
            print('\n\n-----------------------------\n\n')
            if hashcode == signature:
                return HttpResponse(echostr)

        #如果是网页的请求
        else:
            print('\n\n*****************************\n\n')
            return HttpResponse("lele is the most beautiful and beloved dog in the world")

    print('\n\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n')
    webData = request.body
    xmlData = ET.fromstring(webData)
    print(xmlData)
    msg_type = xmlData.find('MsgType').text
    ToUserName = xmlData.find('ToUserName').text
    FromUserName = xmlData.find('FromUserName').text
    CreateTime = xmlData.find('CreateTime').text

    toUser = FromUserName
    fromUser = ToUserName

    if msg_type == "text":
        content = xmlData.find('Content').text
        reply = ReplyText(toUser, fromUser, msg_type, content).send()
        return HttpResponse(reply)

 class ReplyText():
    def __init__(self, to_user, from_user, msg_type, content):
        self.to_user = to_user
        self.from_user = from_user
        self.time = int(time.time())
        self.msg_type = msg_type
        self.content = content

        self.raw_text = """<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime><MsgType><![CDATA[{3}]]></MsgType><Content><![CDATA[{4}]]></Content></xml>"""

    def send(self):
        return self.raw_text.format(self.to_user, self.from_user, self.time, self.msg_type, self.content)   

微信验证:通过; 但是自动回复的功能post请求一直不行,显示http/1.1 301

评论 0