Twisted Web IMPS Echo Client

January 13, 2009

Having a twisted environment, I have been able to write a simple web resource handler. I like twisted, although the documentation is pretty weak, so you have to go down to read the code rather frequently if you want to know how to do things.

My first task was to handle an XML post for IMPS CSP. I thought I would skip the wbxml version to start with.

So first thing was to setup the test client. Just for now, I used curl to post the data:

curl http://localhost:8080/ -d @login-success.in.txt

Where login-success.in.txt is the first message the phone sends in:

<?xml version="1.0"?>
<!DOCTYPE WV-CSP-Message PUBLIC "-//OMA//DTD WV-CSP 1.2//EN" "http://www.openmobilealliance.org/DTD/WV-CSP.DTD">
<WV-CSP-Message xmlns="http://www.wireless-village.org/CSP1.1">
  <Session>
    <SessionDescriptor>
      <SessionType>Outband</SessionType>
    </SessionDescriptor>
    <Transaction>
      <TransactionDescriptor>
        <TransactionMode>Request</TransactionMode>
        <TransactionID>nok1</TransactionID>
      </TransactionDescriptor>
      <TransactionContent xmlns="http://www.wireless-village.org/TRC1.1">
        <Login-Request>
          <UserID>hermes.onesoup</UserID>
          <ClientID>
            <URL>WV:IMPEC01$00001@NOK.S60</URL>
          </ClientID>
          <Password>xxxxxxx</Password>
          <TimeToLive>86400</TimeToLive>
          <SessionCookie>wv:nokia.1789505498</SessionCookie>
        </Login-Request>
      </TransactionContent>
    </Transaction>
  </Session>
</WV-CSP-Message>

The payload is contained within the POST data, but there are not arguments on it (no a=x). So I needed to get the raw content. To achieve that, I created a hello.py file which would contain my Hello resource handler:

from twisted.web.resource import Resource

class Hello(Resource):
    allowedMethods = ('POST',)

    def render_POST(self, request):
        request.content.seek(0, 0)
        data = request.content.read()
        return data

As we can see, all this does is to send the payload in the HTTP POST request back to the client. To get this running on twisted, we write an index.rpy and save it in /Users/brunofr/Sites (adjust):

from imps.csp import hello

# comment the next line in production
reload(hello)
resource = hello.Hello()

A couple of comments about this code:

  1. I put the code within the imps.csp module.
  2. I call reload to allow me to edit the module during development.

And bang, we run it:

twisted -n web --path=/Users/brunofr/Sites

Point your browser to http://localhost:8080/. It does not work, since we are not accepting the ‘GET’ method. But if you use curl:

curl http://localhost:8080/ -d @login-success.in.txt

it works, as it should.


blog comments powered by Disqus