GAE中的处理程序

时间:2012-03-29 10:01:15

标签: python html google-app-engine

我想问一下,我试着打电话给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

编辑: download.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()

在这个页面中,当我上传我的应用程序时,我在此页面上获得此输出(我相信这个页面 - download.py-获取具有URL中的specpic密钥的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

没有保存?

提前致谢。

2 个答案:

答案 0 :(得分:6)

不要在WSGI应用程序中使用print语句 - 并且确实使用WSGI。您需要按照this one之一的App Engine教程,编写正确的WSGI应用程序,将所有代码放在处理程序中(或从处理程序调用),并使用self.response.out.write代替print语句。< / p>

在你掌握如何编写基本的webapp之前,你还需要停止提出几乎相同的问题。

答案 1 :(得分:2)

假设您已发布到/compress.py,请替换

def z(self):

def post(self):

那会让你接下来的问题。

顺便说一句,你可能会发现采取更小的步骤更容易。在这种情况下,一小步就是&#34;我可以通过URL点击处理程序,至少得到一个“hello world&#39;导致?&#34 ;.也许你已经做到了。下一个小步骤是&#34;我可以发布给处理程序并获得一个“hello world”&#39;导致&#34?;这样做(而不是担心)如何处理发布的数据可以解决许多可能出现的问题。通过小步骤逐步解决这些问题。