python - Appengine 上的 file.read() 返回垃圾字符

标签 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()

它在本地工作正常(即返回图像),但是当我在实时服务器上使用它时,我得到了垃圾。您可以在此处查看输出:http://1.ge0.co/fc/1.png

我做错了什么?

谢谢!

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

最佳答案

我最终设法修复了它,我认为问题出在 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())

关于python - Appengine 上的 file.read() 返回垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4816601/

相关文章:

python - 使用 CouchDB-Python 批量取消删除 CouchDB 文档

google-app-engine - App Engine 实例 - 具有相同内存和 cpu 的实例大小有什么区别?

c - 以二进制方式打开文件进行处理

c - 读取文件并存储到数组中

python - numpy数组:快速填充和提取数据

python - 如何在 Windows 命令行中运行 .py 文件?

python - 运行 Google App Engine : unable to open database file 时出错

使用 Google App Engine 的 Facebook 聊天机器人

.net - 在VB.NET中获取文件修改日期

python - 同时使用 Python 3.1 和 2.5