views:

101

answers:

1

Hey,

I am a newcomer to the Python and Twisted game so excuse the ignorance I will likely be asking this question with. As a sort of first program, I am trying to write a basic HTTP server using twisted.web.sever which would simply print to screen the HTTP request, and then print to screen the HTTP response. I am trying to print the entire message. Here is what I have so far:

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
import time

class TestPage(Resource):
    isLeaf = True
    def render_GET(self, request):
        response = "Success"
        print "You're request was %s" % request
        print "The sever's response was %s" % response
        return response

resource = TestPage()
factory = Site(resource)
reactor.listenTCP(8000, factory)
reactor.run()

So far, I am having success printing the request. What I want to know is where I can access the raw response data, not just the textual message. Also, if I wanted to start parsing the request/response for information, what would be the best way to go about doing that?

Edit: I'm also new to stackoverflow, how do I get this code to display properly?

+2  A: 

Take a look at the Request and IRequest API docs to get an idea of what that request parameter offers you. You should be able to find just about everything in the request there.

I'm not sure what you mean by raw response data though. The response is up to you to generate.

Jean-Paul Calderone
Thanks for the pointer to the IRequest API. That had exactly what I was looking for. To clarify my previous question, I guess what I really want is the Request object representing the response from the server. Looking at your answer though, I think I may be confused. My understanding of the framework was that I just write up some generic HTTP response, then twisted goes ahead and adds all the header information later, am I supposed to be adding that header information? If I'm supposed to add it, where is a good place to look for demos on how to do that?
themaestro