user1464409
user1464409

Reputation: 1042

google app engine urlfetch gzip to string

Using Google App Engine, I am trying to urlfetch a gzip file from a URL which contains one csv file.

Ultimately I would like to output the content of the csv file on my webpage.

I have the following code at the moment:

#!/usr/bin/env python
import webapp2

from google.appengine.api import urlfetch

class Test(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    url = *this_is_my_url*
    test = urlfetch.fetch(url, deadline=25)
    self.response.out.write(test.content)

app = webapp2.WSGIApplication([
  ('/test', Test)
], debug=True)

Rather than printing the contents of the file to screen, it asks me to download them locally. How do I stop this local download and instead print directly to the screen/webpage?

Upvotes: 2

Views: 1188

Answers (1)

jan zegan
jan zegan

Reputation: 1657

See if this works.

#!/usr/bin/env python
import webapp2
from google.appengine.api import urlfetch
import gzip
import StringIO

class Test(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    url = *this_is_my_url*

    test = urlfetch.fetch(url, deadline=25)


    f = StringIO.StringIO(test.content)
    c = gzip.GzipFile(fileobj=f)
    content = c.read()

    self.response.out.write(content)

app = webapp2.WSGIApplication([
  (r'/', Test)
], debug=True)

Upvotes: 4

Related Questions