【问题标题】:google app engine urlfetch gzip to string谷歌应用引擎 urlfetch gzip 到字符串
【发布时间】:2012-06-18 17:49:26
【问题描述】:

使用 Google App Engine,我正在尝试从包含一个 csv 文件的 URL 中获取一个 gzip 文件。

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

我现在有以下代码:

#!/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)

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

【问题讨论】:

  • 对我来说似乎是一个错误配置的浏览器,请尝试使用其他浏览器。
  • 您可能想尝试添加一个Content-disposition: inline 标头,尽管理论上它不应该是必需的。
  • 我已经尝试过使用 Firefox、Google Chrome 和 Internet Explorer 并分别下载文件而不是打印到屏幕上。
  • 我将一行更改为: test = urlfetch.fetch(url, headers = {'Content-disposition': 'inline'}, deadline=25) 现在打印到屏幕上,但是是gobbledegook ...下载文件时内容很好,但打印时它们是这样的:o�@����+?�����%UU)q[�
  • 顺便感谢您的快速回复和 cmets!我忘了提到我还必须编辑内容类型行才能让响应写入屏幕。

标签: python google-app-engine gzip


【解决方案1】:

看看这是否有效。

#!/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)

【讨论】:

  • 你是超级巨星扬!这非常有效。非常感谢。
猜你喜欢
  • 2013-04-22
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
相关资源
最近更新 更多