Monday, March 22, 2010

twisted.web2 auto reconnecting browser

Making use web2 Broker Class from http://code.google.com/p/twisted-web2-client

class Browser:
#Init Channel
#Authenticate Channel
#Send Channel
#Recieve Channel
def __init__(self, timeout=180 ):
self.timeout = timeout
self.conn = Broker()
self.headers = {}
self.response_headers = None

def parse_response_headers(self, headers):
self.response_headers = headers
self.raw_headers = headers._raw_headers
if headers.hasHeader('set-cookie'):
parseSetCookies = http_headers.parseSetCookie(headers.getRawHeaders('set-cookie'))
self.headers['Cookie']= parseSetCookies

def getResponseBody(self, resp):
d = defer.Deferred()
self.parse_response_headers(resp.headers)
stack = []
stream_mod.readStream(resp.stream, stack.append).addCallback(lambda x: ''.join(stack)).addCallbacks(d.callback, d.errback)
return d

def connect(self, uri, postdata=None, method='GET', *args, **kwargs):
d = self.conn.ask(uri, method, self.headers, postdata,timeout=self.timeout, *args, **kwargs).addCallback(self.getResponseBody)
return d

No comments:

Post a Comment