源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python调用微信公众平台接口操作示例

  • 时间:2022-01-31 05:51 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python调用微信公众平台接口操作示例
本文实例讲述了Python调用微信公众平台接口操作。分享给大家供大家参考,具体如下: 这里使用的是Django,其他类似
# coding=utf-8
from django.http import HttpResponse
import hashlib, time, re
from xml.etree import ElementTree as ET
def weixin(request):
    token = "your token here"
    params = request.GET
    args = [token, params['timestamp'], params['nonce']]
    args.sort()
    if hashlib.sha1("".join(args)).hexdigest() == params['signature']:
      if params.has_key('echostr'):
      return HttpResponse(params['echostr'])
        else:
          reply = """<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
              <CreateTime>%s</CreateTime>
              <MsgType><![CDATA[text]]></MsgType>
              <Content><![CDATA[%s]]></Content>
              <FuncFlag>0</FuncFlag>
          </xml>"""
          if request.raw_post_data:
              xml = ET.fromstring(request.raw_post_data)
              content = xml.find("Content").text
              fromUserName = xml.find("ToUserName").text
              toUserName = xml.find("FromUserName").text
              postTime = str(int(time.time()))
                if not content:
                  return HttpResponse(reply % (toUserName, fromUserName, postTime, "输入点命令吧..."))
              if content == "Hello2BizUser":
                  return HttpResponse(reply % (toUserName, fromUserName, postTime, "查询成绩绩点请到http://chajidian.sinaapp.com/ 本微信更多功能开发中..."))
              else:
                  return HttpResponse(reply % (toUserName, fromUserName, postTime, "暂不支持任何命令交互哦,功能开发中..."))
      else:
              return HttpResponse("Invalid Request")
  else:
      return HttpResponse("Invalid Request")

更多关于Python相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/636.htm]Python字符串操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/788.htm]Python编码操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/663.htm]Python数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/642.htm]Python函数使用技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/520.htm]Python入门与进阶经典教程[/url]》。 希望本文所述对大家Python程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部