2014-11-24 75 views
-1

我想用plone来接收从微信服务器发布的xml消息。 我定义以下浏览器中查看收到的消息:如何将XML数据发布到plone视图并将xml数据返回给发件人?

class Receive(BrowserView): 
def__init__(self,context,request) 
    self.context = context 
    self.request = request 

def render(self): 
    from zope import event 
    import pdb 
    pdb.set_trace() 
    from my315ok.wechat.events import ReceiveWechatEvent 

    data = self.request.form 
    ev = self.request.environ 
    robot = BaseRoBot(token="haha018")   

    try: 
     rn = robot.check_signature(
     data["timestamp"], 
     data["nonce"], 
     data["signature"] 
     ) 
    except: 
     return self.abort(403) 

    if ev['REQUEST_METHOD'] =="GET": 
     # valid request from weixin 
     if rn: 
      return data["echostr"] 
     else: 
      return self.abort(403)   

    else: 
     # normal request form weixin 
     if not rn: 
      return self.abort(403) 

     body = self.request.get['BODY'] 

     message = parse_user_msg(body) 
     logging.info("Receive message %s" % message)      
     event.notify(ReceiveWechatEvent(message)) 
     return "" 

这是我的ZCML文件:

<configure 
    xmlns="http://namespaces.zope.org/zope" 
    xmlns:browser="http://namespaces.zope.org/browser" 
    i18n_domain="my315ok.wechat"> 

    <browser:page 
     for="*" 
     name="send_as_wechat" 
     class=".send.SendForm" 
     template="templates/send_page.pt" 
     permission="zope2.View" 
    /> 
    <browser:page 
     for="*" 
     name="robot" 
     class=".receive.Receive" 
     permission="zope2.View" 
    /> 
</configure> 

但是当我发送文本格式的XML消息的视图(http://myhost.com/@@robot),遇到了以下错误:

  1. plone运行在调试模式,但调试无法进入渲染功能。

    当我使用tcpdump的捕捉Plone的TCP交通肇事
  2. ,我得到了以下的tcpdump:

问题看喜欢不被Plone的观点后收到的XML请求。

的tcpdump出来:

wechat server to plone:

0x0000: 4500 0101 e7c2 4000 4006 5432 7f00 0001 [email protected]@.T2.... 
0x0010: 7f00 0001 9216 1f90 e80c 8ae5 33f2 5a60 ............3.Z` 
0x0020: 8018 0156 fef5 0000 0101 080a 000a 6e15 ...V..........n. 
0x0030: 000a 6e15 3c78 6d6c 3e3c 546f 5573 6572 ..n.<xml><ToUser 
0x0040: 4e61 6d65 3e3c 215b 4344 4154 415b 7778 Name><![CDATA[wx 
0x0050: 7337 6e6c 6675 6c62 686a 797a 646f 696b s7nlfulbhjyzdoik 
0x0060: 5d5d 3e3c 2f54 6f55 7365 724e 616d 653e ]]></ToUserName> 
0x0070: 3c46 726f 6d55 7365 724e 616d 653e 3c21 <FromUserName><! 
0x0080: 5b43 4441 5441 5b73 6736 3463 4a56 7246 [CDATA[sg64cJVrF 
0x0090: 744c 776d 7958 4d65 374b 4f69 4e54 397a tLwmyXMe7KOiNT9z 
0x00a0: 4155 665d 5d3e 3c2f 4672 6f6d 5573 6572 AUf]]></FromUser 
0x00b0: 4e61 6d65 3e3c 4d73 6754 7970 653e 3c21 Name><MsgType><! 
0x00c0: 5b43 4441 5441 5b74 6578 745d 5d3e 3c2f [CDATA[text]]></ 
0x00d0: 4d73 6754 7970 653e 3c43 6f6e 7465 6e74 MsgType><Content 
0x00e0: 3e3c 215b 4344 4154 415b 6861 6861 5d5d ><![CDATA[haha]] 
0x00f0: 3e3c 2f43 6f6e 7465 6e74 3e3c 2f78 6d6c ></Content></xml 
0x0100: 3e          > 

plone to wechat server:

0x00d0: 656e 742d 5479 7065 3a20 7465 7874 2f68 ent-Type:.text/h 
0x00e0: 746d 6c3b 2063 6861 7273 6574 3d75 7466 tml;.charset=utf 
0x00f0: 2d38 0d0a 0d0a 3c68 746d 6c3e 0a3c 6865 -8....<html>.<he 
0x0100: 6164 3e3c 7469 746c 653e 5369 7465 2045 ad><title>Site.E 
0x0110: 7272 6f72 3c2f 7469 746c 653e 3c2f 6865 rror</title></he 
0x0120: 6164 3e0a 3c62 6f64 7920 6267 636f 6c6f ad>.<body.bgcolo 
0x0130: 723d 2223 4646 4646 4646 223e 0a0a 0a20 r="#FFFFFF">.... 
0x0140: 203c 6832 3e53 6974 6520 4572 726f 723c .<h2>Site.Error< 
0x0150: 2f68 323e 0a0a 2020 3c70 3e41 6e20 6572 /h2>....<p>An.er 
0x0160: 726f 7220 7761 7320 656e 636f 756e 7465 ror.was.encounte 
0x0170: 7265 6420 7768 696c 6520 7075 626c 6973 red.while.publis 
0x0180: 6869 6e67 2074 6869 7320 7265 736f 7572 hing.this.resour 
0x0190: 6365 2e0a 2020 3c2f 703e 0a20 200a 2020 ce....</p>...... 
0x01a0: 3c70 3e0a 2020 2020 2020 3c73 7472 6f6e <p>.......<stron 
0x01b0: 673e 4572 726f 7220 5479 7065 3a20 5265 g>Error.Type:.Re 
0x01c0: 7370 6f6e 7365 4572 726f 723c 2f73 7472 sponseError</str 
0x01d0: 6f6e 673e 3c62 722f 3e0a 2020 2020 2020 ong><br/>....... 
0x01e0: 3c73 7472 6f6e 673e 4572 726f 7220 5661 <strong>Error.Va 
0x01f0: 6c75 653a 2052 6573 706f 6e73 6545 7272 lue:.ResponseErr 
0x0200: 6f72 2829 3c2f 7374 726f 6e67 3e3c 6272 or()</strong><br 
0x0210: 2f3e 200a 2020 3c2f 703e 0a20 0a20 203c />....</p>.....< 
0x0220: 6872 206e 6f73 6861 6465 3d22 6e6f 7368 hr.noshade="nosh 
0x0230: 6164 6522 202f 3e0a 200a 2020 3c70 3e54 ade"./>.....<p>T 
0x0240: 726f 7562 6c65 7368 6f6f 7469 6e67 2053 roubleshooting.S 
0x0250: 7567 6765 7374 696f 6e73 3c2f 703e 0a0a uggestions</p>.. 

感谢很多。

我也试着用__call__(self)替换render(self),但还是不行。 顺便说一句,我使用一个真正的浏览器代理访问http://myhost.com/@@robot,(例如,IE火狐等)可进入render(self)功能调试

+0

您是否尝试覆盖__call __(self)方法而不是render(self)? – SauZheR 2014-11-24 14:13:11

+0

'render'方法默认调用__call__'方法。你是如何注册浏览器视图的?通过zcml?你使用'template'属性,还是只使用'class'属性?我猜可能有问题,为什么你的渲染方法没有得到执行。 – Mathias 2014-11-24 15:30:31

+0

向我们展示您的ZCML。 – SteveM 2014-11-24 15:56:50

回答

0

你的示例代码并不一致。您定义一个名为“机器人”的视图,并使用类值“.receive.Robot”。这不适合您的类定义,其中类称为“接收”。

反正 - 适合您的configure.zcml的receive.py文件的内容应该是这样的:

from Products.Five import BrowserView 

class Receive(BrowserView): 
    """ some docstring """ 

    def __call__(self): 
     """ gets called and returns incoming form data """ 
     return "this was the incoming form data: %s" % (self.request.form) 

访问您的浏览器应该返回incomfing数据视图。

我不确定是否理解你的最后一个注释是否正确:如果你的代码已经在使用浏览器而不是通过微信服务器 - 比检查你的访问日志(通常位于$ buildout-home/var/log/instance-Z2.log)如果请求正在接收plone实例。

另一个问题可能是,您需要身份验证 - 但这取决于您的设置。您在通话的微信环境中看到任何结果吗?

+0

其实在IE浏览器访问视图的时候,所有的东西都是正常的。但是微信服务器的后期xml请求视图,不行! – adam 2014-11-26 10:44:20

+0

这是plone日志:127.0.0.1 - Anonymous [29/Nov/2014:09:12:50 +0800]“POST/xsgs998/@@ robot?timestamp = 1417223570&nonce = evbownvd7tqstfpxwdeguragk3zkmjni&signature = 7aecdf55efe035bf6b80968848afccfa7c75f5c1 HTTP/1.1”500 1001“” “Ruby” 127.0.0.1 - Anonymous [29/Nov/2014:09:30:52 +0800]“GET/xsgs998/@@ robot HTTP/1.1”200 816“”“Mozilla/5.0(X11; Linux x86_64; rv:22.0)Gecko/20100101 Firefox/22.0“第一项是微信服务器访问日志;其次是Firefox访问日志。微信服务器发布请求没有到达视图功能。 – adam 2014-11-29 01:36:04