代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# filename: handle.py
import hashlib
import web
import event
import receive
import time
import os
class Handle(object):
def __init__(self):
# os.path.dirname功能:去掉文件名,返回目录
# __file__表示了当前文件的path
self.app_root = os.path.dirname(__file__)
# os.path.join()函数用于路径拼接文件路径,可以传入多个路径。
self.templates_root = os.path.join(self.app_root, 'templates')
self.render = web.template.render(self.templates_root)
def GET(self):
try:
data = web.input()
print(data)
if len(data) == 0:
return "hello, this is handle view"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "token"
list = [token, timestamp, nonce]
list.sort()
s = list[0] + list[1] + list[2]
hashcode = hashlib.sha1(s.encode('utf-8')).hexdigest()
print("handle/GET func: hashcode, signature: ", hashcode, signature)
if hashcode == signature:
return echostr
else:
return echostr
except Exception as Argument:
return Argument
def POST(self):
try:
webData = web.data()
print("Handle Post webdata is:\n", webData)
# 打印消息体日志
recMsg = receive.parse_xml(webData)
# 文字类消息
if isinstance(recMsg, receive.Msg) and recMsg.MsgType == 'text':
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
content = "这是一个demo(只回复text类消息)" + str(recMsg.Content)
print('Reply message info:\n')
print('toUser =', toUser)
print('fromUser = ', fromUser)
print('content = ', content)
return self.render.reply_text(toUser, fromUser, int(time.time()), content)
# 菜单点击类信息
elif isinstance(recMsg, receive.Event) and recMsg.MsgType == 'event':
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
eventKey = recMsg.Eventkey
content = event.checkEvent_id(eventKey)
print('Reply message info:\n')
print('toUser =', toUser)
print('fromUser = ', fromUser)
print('content = ', content)
return self.render.reply_text(toUser, fromUser, int(time.time()), content)
else:
print("不支持的消息类型:", recMsg.MsgType)
return "success"
except Exception as Argment:
return Argment
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。