Gzipped Response with web.client.Agent
Hello, I've putted together examples from the web (see below) to replace getPage with web.client.Agent and it works fine. Now, I'd like to get gzipped response, but I can't get gunzipping to work before returning result. Thanks for help! class StringGzipReceiver(Protocol): def __init__(self): self.string = None self.deferred = defer.Deferred() def dataReceived(self, bytes): print "dataReceived" print type(bytes) if self.string: self.string += bytes else: self.string = bytes def connectionLost(self, reason): if reason.check(ResponseDone) or reason.check(PotentialDataLoss): gzipper = gzip.GzipFile(fileobj=self.string) gz = gzipper.read() result = unicode(gz, 'UTF-8') self.deferred.callback(result) else: self.deferred.errback(reason) class StringReceiver(Protocol): def __init__(self): self.string_io = codecs.getwriter('utf_8')(StringIO()) self.deferred = defer.Deferred() def dataReceived(self, bytes): self.string_io.write(bytes) def connectionLost(self, reason): if reason.check(ResponseDone) or reason.check(PotentialDataLoss): self.deferred.callback(self.string_io.getvalue()) else: self.deferred.errback(reason) class StringProducer(object): implements(IBodyProducer) def __init__(self, body): self.body = body self.length = len(body) def startProducing(self, consumer): consumer.write(self.body) return succeed(None) def pauseProducing(self): pass def stopProducing(self): pass def SearchHotelsByID(): host = 'demo.com' postdata = 'some data' headers = { 'Host' : [host], 'Accept-Encoding' : ['gzip'] } def cbRequest(response): stringReceiver = StringGzipReceiver() response.deliverBody(stringReceiver) return stringReceiver.deferred def _noPage(failure): print "Error: %s" % failure.getErrorMessage() print failure.getTraceback() return failure agent = Agent(reactor) d = agent.request( 'POST', url, headers=Headers(headers), bodyProducer=StringProducer(postdata) ) d.addCallback(cbRequest) d.addErrback(_noPage) d.addBoth(finish) return d
participants (2)
-
exarkun@twistedmatrix.com
-
Vokdin Sergei