Django开发微信公众平台
处理微信发来的信息,实际上就是处理xml的过程。先写xml工具类
# -*- coding:utf-8 -*-
from xml.dom import minidom
from Web.model.WeiXin import * def get_attrvalue(node, attrname):
return node.getAttribute(attrname) if node else '' def get_nodevalue(node, index = 0):
return node.childNodes[index].nodeValue if node else '' def get_xmlnode(node,name):
return node.getElementsByTagName(name) if node else [] def xml_to_string(xml):
doc = minidom.parse(xml)
return doc.toxml('UTF-8') def get_xml_data(xml):
doc = minidom.parseString(xml)
root = doc.documentElement
ToUserName = get_nodevalue(get_xmlnode(root,'ToUserName')[0]).encode('utf-8','ignore')
FromUserName = get_nodevalue(get_xmlnode(root,'FromUserName')[0]).encode('utf-8','ignore')
CreateTime = get_nodevalue(get_xmlnode(root,'CreateTime')[0]).encode('utf-8','ignore')
MsgType = get_nodevalue(get_xmlnode(root,'MsgType')[0]).encode('utf-8','ignore')
Content = get_nodevalue(get_xmlnode(root,'Content')[0]).encode('utf-8','ignore')
MsgId = get_nodevalue(get_xmlnode(root,'MsgId')[0]).encode('utf-8','ignore')
return WeiXin(ToUserName=ToUserName,FromUserName=FromUserName,CreateTime=CreateTime,MsgType=MsgType,Content=Content,MsgId=MsgId) if __name__ =='__main__':
xml = '<xml><ToUserName><![CDATA[gh_d740805e3dda]]></ToUserName><FromUserName><![CDATA[otvK9uEl5TdgfKaDtBhSb6CM49Xs]]></FromUserName><CreateTime>1426579233</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[哈哈]]></Content><MsgId>6127111151090981083</MsgId></xml>'
print get_xml_data(xml)
封装一个微信xml实体类:
__author__ = 'zan'
class WeiXin():
def __init__(self,ToUserName,FromUserName,CreateTime,MsgType,Content,MsgId):
self.ToUserName= ToUserName
self.FromUserName= FromUserName
self.CreateTime= CreateTime
self.MsgType= MsgType
self.Content= Content
self.MsgId= MsgId
def __repr__(self):
return 'ToUserName=%s,\n FromUserName=%s,\nCreateTime=%s,\n,MsgType=%s,\nContent=%s,\nMsgId=%s\n' %(self.ToUserName,self.FromUserName,self.CreateTime,self.MsgType,self.Content,self.MsgId)
最后解析xml,并返回xml
def weixin(requset):
print 'requset.body=========%s'%requset.body
if not requset.body.strip():
return HttpResponse('error')
weixinObj = get_xml_data(requset.body)
print '解析之前 %s'%weixinObj
toUserName = weixinObj.FromUserName;
fromUserName = weixinObj.ToUserName;
weixinObj.FromUserName = fromUserName;
weixinObj.ToUserName = toUserName;
print '解析之后 %s'%weixinObj
result = '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><MsgId>%s</MsgId></xml>'%(weixinObj.ToUserName,weixinObj.FromUserName,weixinObj.CreateTime,weixinObj.MsgType,weixinObj.Content,weixinObj.MsgId)
return HttpResponse(result)
Django开发微信公众平台的更多相关文章
- django开发微信公众平台遇到的问题记录
在pythonanywhere.com上使用django开发微信公众平台应用,结果用户发送的信息,微信服务器一次也没有成功转发到pythonanywhere上来,但是用接口测试工具调试却发现是正常的, ...
- [c#]asp.net开发微信公众平台(1)数据库设计
开发微信公众平台之前,先去微信官方了解下大概的情况 这里:http://mp.weixin.qq.com/wiki/index.php :看了之后心里大致有数了,开始设计数据库,尽可能的考虑,未考虑到 ...
- [C#]asp.net开发微信公众平台----目录汇总-持续更新
1.[c#]asp.net微信公众平台开发(1)数据库设计 2.[c#]asp.net微信公众平台开发(2)多层架构框架搭建和入口实现 3.[c#]asp.net微信公众平台开发(3)微信消息封装及反 ...
- [c#]asp.net开发微信公众平台(8)微信9大高级接口,自定义菜单
前7篇把最基础的消息接收和回复全做完了, 也把高级接口的入口和分拆处理写好了空方法, 此篇接着介绍微信的9大高级接口, 并着重讲解其中的自定义菜单. 微信9大接口为: 1.语音识别接口 2.客服接 ...
- [c#]asp.net开发微信公众平台(7)前6篇的整体框架demo源码
这里给出的demo是具备整体框架的微信公众平台源码, 所谓demo就是拿过去就可以直接演示使用的东西, 当然不会具备非常详细的具体到业务层面.数据层面的东西, 每个人都可以在此基础上自由发挥, 只 ...
- [c#]asp.net开发微信公众平台(6)阶段总结、服务搭建、接入
经过前5篇,跟着一步步来的话,任何人都能搭建好一个能处理各种微信消息的框架了,总结一下最容易忽略的问题: 1.文本消息中可以使用换行符\n : 2.微信发来的消息中带的那个长整型的时间,我们完全 ...
- [c#]asp.net开发微信公众平台(5)微信图文消息
上篇已经成功响应了关注事件,也实现了文本消息的发送,这篇开始图文消息处理, 微信中最常用的消息类型就是图文消息了,因为它图文并茂,最能表达信息. 图文消息在微信中的接口定义如下: <xml> ...
- C#开发微信公众平台-就这么简单(附Demo)转载
C#开发微信公众平台-就这么简单(附Demo) 来源:https://www.cnblogs.com/xishuai/p/3625859.html#!comments 写在前面 阅读目录: 服务号和 ...
- 使用Java开发微信公众平台(二)——消息的接收与响应
上一篇文章(http://www.jerehedu.com/fenxiang/171807_for_detail.htm )中,我们学习了使用Java语言开发微信公众平台的第一部分——环境搭建与开发接 ...
随机推荐
- PHP杂技(一)
逻辑运算符 &&和& ||和|的部分区别 返回结果类型不同, A||B 如果A为真那么B不会做判断,而A|B前后都做判断 switch判断中并不是===,更像是==,例如(1) ...
- Bash Command 1: find
GNU find searches the directory tree rooted at each given starting-point by evaluating the given exp ...
- Educational Codeforces Round 11——C. Hard Process(YY)
C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- mybatis学习(十)——缓存介绍
与Hibernate一样,MyBatis 也提供了一级缓存和二级缓存的支持. 1.一级缓存:(本地缓存)SqlSession级别的缓存,默认一直开启的 , 与数据库同一次会话期间的数据会放到本地缓存中 ...
- Eclipse + Jersey 发布RESTful WebService(一)了解Maven和Jersey,创建一个WS项目(成功!)
一.下文中需要的资源地址汇总 Maven Apache Maven网站 http://maven.apache.org/ Maven下载地址: http://maven.apache.org/down ...
- 关于sudo dpkg-divert –local –rename –add /sbin/initctl导致的开机无图标解决方法
背景: ubutnu16.04 使用status docker,发现 无法连接到 status: Unable to connect to Upstart: Failed to connect to ...
- leetcode 20 简单括号匹配
栈的运用 class Solution { public: bool isValid(string s) { stack<char>The_Stack; ; The_Stack.push( ...
- Thrift & RPC介绍
在学习thrift之前,先来看一下什么是rpc rpc远程过程调用,通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC采用客户机/服务器模式.请求程序就是一个客户机,而服务提供 ...
- MySQL 5.7.17绿色版安装
下载地址 :https://dev.mysql.com/downloads/mysql/ ,需要oracle帐号 下载 Windows (x86, 64-bit), ZIP Archive 是个 ...
- Change visual studio 2015 enterprise installation path(转)
I would like to install VS2015 in a drive different than C:. The problem is that when I run the inst ...