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