Question

I am Trying to Build a basics echo http server and http clint with twisted, here is my example, this is not what i have written , iam just trying to understand the handshaking of these two implementations.

this is my client.py

from StringIO import StringIO

from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

from twisted.web.client import FileBodyProducer

agent = Agent(reactor)
body = FileBodyProducer(StringIO("hello, world"))
d = agent.request(
    'GET',
    'http://example.com/',
    Headers({'User-Agent': ['Twisted Web Client Example'],
         'Content-Type': ['text/x-greeting']}),
    body)

def cbResponse(ignored):
   print 'Response received'
d.addCallback(cbResponse)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

this is my server.py

from twisted.web import server, resource
from twisted.internet import reactor

class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        print request

site = server.Site(Simple())
reactor.listenTCP(8080, site) 
reactor.run()

i want to print on the server console. the message in body in client hello world. what should be added in server side.

Was it helpful?

Solution

Request.content:

def render_GET(self, request):
    print request.content.read()

See http://twistedmatrix.com/documents/current/api/twisted.web.iweb.IRequest.html#content

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top