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

Where is the first message the phone sends in:

<?xml version="1.0"?>
<!DOCTYPE WV-CSP-Message PUBLIC "-//OMA//DTD WV-CSP 1.2//EN" "">
<WV-CSP-Message xmlns="">
      <TransactionContent xmlns="">

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 file which would contain my Hello resource handler:

from twisted.web.resource import Resource

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

    def render_POST(self, request):, 0)
        data =
        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
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

it works, as it should.

blog comments powered by Disqus