【问题标题】:Handlers in GAEGAE 中的处理程序
【发布时间】:2012-04-12 23:12:36
【问题描述】:

我想问一下,我试图打电话给webapp.RequestHandler,但是这个处理程序没有调用: 这是 compress.py 页面:

from __future__ import with_statement
from google.appengine.api import files
import cgi, cgitb ; cgitb.enable()
import StringIO
import zipfile
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app
class zip(webapp.RequestHandler):
    def z(self):
        form = cgi.FieldStorage()
        zipstream=StringIO.StringIO()
        zfile = zipfile.ZipFile(file=zipstream,mode="w",compression=zipfile.ZIP_DEFLATED)     
        file_upload = form['file[]']
        data=file_upload.file.read()
        filename2 = file_upload.filename
        zfile.writestr(filename2,data)
        zfile.close()
        zipstream.seek(0)
        zip_file = files.blobstore.create(mime_type='application/zip',_blobinfo_uploaded_filename='test.zip')
        with files.open(zip_file, 'a') as f:
            f.write(zipstream.getvalue())
        files.finalize(zip_file)
        blob_key = files.blobstore.get_blob_key(zip_file)        
        self.response.out.write('<a href="download.py?key=%s">get zip</a>' %blob_key)

def main():
     application = webapp.WSGIApplication( [(r'/compress.py', zip)], debug=True)
     run_wsgi_app(application)
if __name__ == "__main__":
    main()

app.yaml:

application: my application
version: 1
runtime: python
api_version: 1

handlers:
- url: (.*)/
  static_files: static\1/index.html
  upload: static/index.html
- url: /compress.py
  script: compress.py
- url: /download.py
  script: download.py
- url: /decompress.py
  script: decompress.py

项目结构:

/index.html
/compress.py
/download.py

编辑: 下载.py:

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app
import urllib
from mimetypes import guess_type

def mime_type(filename):
    return guess_type(filename)[0]

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self):
        print "Doaa"
        blob_key = self.request.get('key')
        blob_key = str(urllib.unquote(blob_key))
        blob_info = blobstore.BlobInfo.get(blob_key)
        content_type1 =mime_type(blob_info.filename)
        save_as1 =  blob_info.filename
        self.send_blob(blob_info,content_type=content_type1,save_as=save_as1)



def main():

    #application = webapp.WSGIApplication([('/',ServeHandler),], debug=True)
    application = webapp.WSGIApplication([
            (r'/download.*', ServeHandler),
        ], debug=True)
    run_wsgi_app(application)
if __name__ == '__main__':
    main()

现在在这个页面上,当我上传我的应用程序时,我在这个页面上得到了这个输出(我相信这个 page-download.py-获取在 URL 中具有特定密钥的 blob 并将这个文件下载到我的 PC),但结果为:

Status: 200 OK Cache-Control: no-cache X-AppEngine-BlobKey: AMIfv96uuwRiM-nYO7sp7nPk5Ny0IDv1mrVCkBhFMPn9AUea4rRg5x8sVWlLFJNQ2PxSKD2s6VNVjiPPZFDyP33EegP_QzLYQEnHdSSj_qindkuqeWB7YYnSeReBDYWDAAOf566LCSyWrXBUPq0Z_NiGtZjyvM3-5exv3TxIOYc9PBYuTQ3Vpww Content-Type: application/zip Content-Disposition: attachment; filename="test.zip" Expires: Fri, 01 Jan 1990 00:00:00 GMT Content-Length: 0

不保存?

提前致谢。

【问题讨论】:

  • -1 用于代码截图。认真的吗?
  • @Adam Crossland:为什么,我把我的代码放在另一个问题上(不是截图)但问题仍然存在。
  • 出于尼克在回答中概述的原因。
  • @Adam Crossland:我编辑了我的问题对这个问题有什么建议吗?
  • @Daniel Roseman:我现在编辑了我的问题??

标签: python html google-app-engine


【解决方案1】:

不要在 WSGI 应用程序中使用打印语句 - 并且要使用 WSGI。您需要遵循 App Engine 教程之一,例如 this one,编写适当的 WSGI 应用程序,将所有代码放入处理程序(或从处理程序调用),并使用 self.response.out.write 而不是打印语句。

你还需要停止问几乎相同的问题,直到你很好地掌握了如何编写一个基本的 web 应用程序。

【讨论】:

  • :我按照你说的尝试了,将类 zip 定义为 :class zip(webapp.RequestHandler):,主要我称之为:run_wsgi_app( webapp.WSGIApplication( [('/', zip),], debug=True)),但是现在什么都没有执行??
【解决方案2】:

假设你发帖到/compress.py,替换

def z(self):

def post(self):

这会让你解决下一个问题。

顺便说一句,您可能会发现采取更小的步骤更容易。在这种情况下,一小步将是“我可以通过 URL 访问处理程序并至少获得 'hello world' 结果吗?”。也许你已经这样做了。下一个小步骤是“我可以发布到处理程序并获得'hello world'结果吗?”这样做而不用担心(还)如何处理已发布的数据可以消除许多可能的问题。一点一点地解决这些问题。

【讨论】:

  • :非常感谢您的帮助。
  • 如果您要玩 blob,那么一个非常方便的处理程序就是显示所有 BlobInfo 实体,包括键、mime 类型和大小。我想你会发现你已经设法存储了一个长度为零的 blob。
  • @Dave W. Smith:但是当我检查我的应用程序的 blobstore 时,文件存储成功??
猜你喜欢
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多