Python調(diào)用微信公眾平臺接口操作示例
本文實例講述了Python調(diào)用微信公眾平臺接口操作。分享給大家供大家參考,具體如下:
這里使用的是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/ 本微信更多功能開發(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è)計有所幫助。
相關(guān)文章
jupyter notebook 參數(shù)傳遞給shell命令行實例
這篇文章主要介紹了jupyter notebook 參數(shù)傳遞給shell命令行實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python使用Bokeh庫實現(xiàn)實時數(shù)據(jù)的可視化
Python語言以其豐富的數(shù)據(jù)科學(xué)生態(tài)系統(tǒng)而聞名,其中Bokeh庫作為一種功能強(qiáng)大的可視化工具,為實時數(shù)據(jù)的可視化提供了優(yōu)秀的支持,本文將介紹如何使用Bokeh庫實現(xiàn)實時數(shù)據(jù)的可視化,并提供相關(guān)代碼實例,需要的朋友可以參考下2024-05-05
詳解Django-restframework 之頻率源碼分析
這篇文章主要介紹了Django-restframework 之頻率源碼分析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02

