common.py 761 B

123456789101112131415161718192021222324252627
  1. import web
  2. from wechatpy.crypto import WeChatCrypto
  3. from wechatpy.exceptions import InvalidSignatureException
  4. from wechatpy.utils import check_signature
  5. from config import conf
  6. MAX_UTF8_LEN = 2048
  7. class WeChatAPIException(Exception):
  8. pass
  9. def verify_server(data):
  10. try:
  11. signature = data.signature
  12. timestamp = data.timestamp
  13. nonce = data.nonce
  14. echostr = data.get("echostr", None)
  15. token = conf().get("wechatmp_token") # 请按照公众平台官网\基本配置中信息填写
  16. check_signature(token, signature, timestamp, nonce)
  17. return echostr
  18. except InvalidSignatureException:
  19. raise web.Forbidden("Invalid signature")
  20. except Exception as e:
  21. raise web.Forbidden(str(e))