Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python GAE中的处理程序_Python_Html_Google App Engine - Fatal编程技术网

Python GAE中的处理程序

Python GAE中的处理程序,python,html,google-app-engine,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

我想问,我试图调用
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()
项目结构:

/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()
现在在这个页面中,当我上传我的应用程序时,我在这个页面上得到了这个输出(我相信这个页面-download.py-get在URL中有specfic键的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
没有储蓄


提前感谢。

不要在WSGI应用程序中使用打印语句,一定要使用WSGI。您需要遵循一个应用程序引擎教程,例如,编写一个合适的WSGI应用程序,将所有代码放入处理程序中(或从处理程序调用),并使用
self.response.out.write
而不是print语句


您还需要停止问几乎相同的问题,直到您对如何编写基本Web应用程序有了良好的掌握。

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

def z(self):

这会让你面临下一个问题


顺便说一句,你可能会发现采取更小的步骤更容易。本例中的一小步是“我可以通过URL点击处理程序并至少得到一个‘hello world’结果吗?”。也许你已经这么做了。下一个小步骤是“我可以发布到处理程序并获得“hello world”结果吗?”这样做完全不必担心如何处理发布的数据,从而消除了许多可能的问题。一步一步地解决这些问题。

-1查看代码截图。真的吗?@Adam Crossland:为什么,我把我的代码放在了另一个问题上(不是截图),但问题仍然存在。出于Nick在回答中概述的原因。@Adam Crossland:我编辑了我的问题,你对这个问题有什么建议吗?@Daniel Roseman:我现在编辑了我的问题???:我试着像你说的那样,将类zip定义为:类zip(webapp.RequestHandler):,大体上我称之为:run_wsgi_app(webapp.WSGIApplication([('/',zip),],debug=True)),但现在什么也没有执行???:非常感谢您的帮助。如果您要玩Blob,一个非常方便的处理程序可以显示所有BlobInfo实体,包括键、mime类型和大小。我想你会发现你已经成功地存储了一个零长度的blob。@戴夫·W·史密斯:但是当我检查应用程序的blob存储时,文件成功地存储了??
def z(self):
def post(self):