微信公众平台接口Python使用样例代码
澄迈中立科技 | 2018-01-26 23:58:14 | 阅读:41404
#coding=utf-8fromdjango.httpimportHttpResponseimporthashlib,time,refromxml.etreeimportElementTreeasETdefweixin(request):token="yourtokenhere"params=request.GETargs=[token,params['timestamp'],params['nonce']]args.sort()ifhashlib.sha1("".join(args)).hexdigest()==params['signature']:ifparams.has_key('echostr'):returnHttpResponse(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>"""ifrequest.raw_post_data:xml=ET.fromstring(request.raw_post_data)content=xml.find("Content").textfromUserName=xml.find("ToUserName").texttoUserName=xml.find("FromUserName").textpostTime=str(int(time.time()))ifnotcontent:returnHttpResponse(reply%(toUserName,fromUserName,postTime,"输入点命令吧..."))ifcontent=="Hello2BizUser":returnHttpResponse(reply%(toUserName,fromUserName,postTime,"产看微信营销经验,请登录xxxx"))else:returnHttpResponse(reply%(toUserName,fromUserName,postTime,"暂不支持任何命令交互哦,功能开发中..."))else:returnHttpResponse("InvalidRequest")else:returnHttpResponse("InvalidRequest")