谷歌应用程序引擎urlfetch gzip字符串 [英] google app engine urlfetch gzip to string

查看:63
本文介绍了谷歌应用程序引擎urlfetch gzip字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google App Engine,我试图从包含一个csv文件的URL中urlfetch一个gzip文件。



最终,我想输出csv文件在我的网页上。



我现在有以下代码:

 #!/ usr / bin / env python 
import webapp2
$ b from google.appengine.api import urlfetch
$ b $ 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)

与将文件内容打印到屏幕它要求我在本地下载它们。如何停止本地下载,直接打印到屏幕/网页?

解决方案

看看是否有效。

 #!/ usr / bin / env python 
从google.appengine.api导入webapp2
导入urlfetch
import gzip
import StringIO
$ b $ 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)
pre>

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?

解决方案

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)

这篇关于谷歌应用程序引擎urlfetch gzip字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆