How to post XML data to plone view and return xml data to sender?

199 views Asked by At

I want to using plone to receive to xml message that posted from Wechat server. I define the following browser view to receive message:

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 ""

This is my zcml file:

 <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>

But when I send text xml message to the view(http://myhost.com/@@robot), encountered the following error:

  1. plone run at debug model,but debug can't enter render funcation.

  2. when I use tcpdump catch plone tcp traffice ,I got the following tcpdump:

the problem look to like post xml request can't been received by plone view.

tcpdump out:

wechat server to plone:

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

Thanks lot.

I also try to using __call__(self) replace render(self),but still don't work. By the way, I using a real browser agent to access the http://myhost.com/@@robot,(for example,IE firefox etc.)may enter the render(self) function for debug

1

There are 1 answers

2
jkubaile On

Your example code is not consistent. You define a view called "robot" and are using a the class value ".receive.Robot". That doesn't suit to your class definition where the class is called "Receive".

Anyway - suitable to your configure.zcml the content of the receive.py file should be like this:

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)

Accessing the view with your Browser should return the incomfing data.

I am not sure if I understand your last note right: if your code is already working with browser, but not via the Wechat Server - than check your access logs (in general, located at $buildout-home/var/log/instance-Z2.log) if the requests are receiving the plone instance.

Another problem might be, that your need Authentication - but that depends on your setup. Do you see any results in the Wechat environment of the call?