GAE中的处理程序

Com*_*eer -5 html python 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()
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

项目结构:

/index.html
/compress.py
/download.py
Run Code Online (Sandbox Code Playgroud)

编辑: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()
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

没有保存?

提前致谢.

Nic*_*son 6

不要在WSGI应用程序中使用print语句 - 并且确实使用WSGI.您需要按照App Engine的教程,如一个这一个,写一个适当的WSGI应用程序,把所有的代码内部处理程序(或处理程序调用),并使用self.response.out.write替代打印报表.

在掌握如何编写基本Web应用程序之前,您还需要停止询问几乎相同的问题.