本文實(shí)例講述了Python調(diào)用微信公眾平臺接口操作。分享給大家供大家參考,具體如下:
這里使用的是Django,其他類似
# coding=utf-8from django.http import HttpResponseimport hashlib, time, refrom xml.etree import ElementTree as ETdef 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, "輸入點(diǎn)命令吧..."))              if content == "Hello2BizUser":                  return HttpResponse(reply % (toUserName, fromUserName, postTime, "查詢成績績點(diǎn)請到http://chajidian.sinaapp.com/ 本微信更多功能開發(fā)中..."))              else:                  return HttpResponse(reply % (toUserName, fromUserName, postTime, "暫不支持任何命令交互哦,功能開發(fā)中..."))      else:              return HttpResponse("Invalid Request")  else:      return HttpResponse("Invalid Request")更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》。
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選