-1

我想使用 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>

但是当我向视图(http://myhost.com/@@robot)发送文本 xml 消息时,遇到以下错误:

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

  2. 当我使用 tcpdump catch plone tcp traffice 时,我得到了以下 tcpdump:

plone 视图无法接收到 post xml 请求的问题。

tcpdump 输出:

微信服务器克隆:

    0x0000:  4500 0101 e7c2 4000 4006 5432 7f00 0001  E.....@.@.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                                       >

克隆到微信服务器:

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)replace render(self),但仍然无法正常工作。 顺便说一下,我用一个真正的浏览器代理访问http://myhost.com/@@robot,(例如,IE firefox等)可以进入 render(self)调试功能

4

1 回答 1

0

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

无论如何 - 适合您的 configure.zcml 接收.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)

使用浏览器访问视图应该会返回传入的数据。

我不确定我是否理解您的最后一条提示:如果您的代码已经在浏览器上运行,但不是通过微信服务器 - 请检查您的访问日志(通常位于 $buildout-home/var/log/instance- Z2.log) 如果请求正在接收克隆实例。

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

于 2014-11-26T08:32:21.313 回答