Python appengine上的file.read()返回垃圾字符

Python appengine上的file.read()返回垃圾字符,python,google-app-engine,file-io,Python,Google App Engine,File Io,我正在使用以下代码: class Image(webapp.RequestHandler): def get(self, id): path = os.path.join(os.path.dirname(__file__), 'fcimages/%s.png' % id) self.response.headers['Content-Type'] = 'image/png' print file(path,'rb').read() 它在本

我正在使用以下代码:

class Image(webapp.RequestHandler):
    def get(self, id):
        path = os.path.join(os.path.dirname(__file__), 'fcimages/%s.png' % id)
        self.response.headers['Content-Type'] = 'image/png'
        print file(path,'rb').read()
它在本地运行良好,即返回图像,但当我在实时服务器上使用它时,会收到垃圾邮件。您可以在此处看到输出:

我做错了什么

谢谢


PS-我知道这不是最健壮的代码,但它仅用于我的内部项目,它不是按比例构建的。

看起来内容类型设置不正确

Content-Length:72643
Content-Type:text/html
Date:Thu, 27 Jan 2011 13:01:57 GMT
Server:Google Frontend

这将解释为什么您看到的是文件的内容而不是渲染的图像。

看起来内容类型设置不正确

Content-Length:72643
Content-Type:text/html
Date:Thu, 27 Jan 2011 13:01:57 GMT
Server:Google Frontend

这可以解释为什么您看到的是文件的内容而不是渲染的图像。

我最终设法解决了它,我认为问题在于self.response和print的结合。以下是有效的代码:

class Image(webapp.RequestHandler):
    def get(self, id):
        path = os.path.join(os.path.dirname(__file__), 'fcimages/%s.png' % id)
        self.response.headers['Content-Type'] = "image/png"
        self.response.out.write(file(path, 'rb').read())

我最终设法解决了这个问题,我认为问题在于自我反应和打印的结合。以下是有效的代码:

class Image(webapp.RequestHandler):
    def get(self, id):
        path = os.path.join(os.path.dirname(__file__), 'fcimages/%s.png' % id)
        self.response.headers['Content-Type'] = "image/png"
        self.response.out.write(file(path, 'rb').read())

我将原始代码中的第4行更改为:print Content Type:image/png,现在我得到了一个空白页面,而不是垃圾代码。。。还有其他提示吗?我将原始代码中的第4行更改为:print Content Type:image/png,现在我得到了一个空白页面,而不是垃圾代码。。。还有什么建议吗?是的。切勿在webapp RequestHandler中使用打印。您将打印到stdout而不是响应对象,打印的内容将最终显示在原始CGI响应中,通常隐藏在标题中。是的。切勿在webapp RequestHandler中使用打印。您将打印到stdout而不是响应对象,打印的内容将最终显示在原始CGI响应中,通常隐藏在标题中。