I want to ask,i tried to call webapp.RequestHandler,but this handler didn’t called:
this is compress.py page:
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
Project structure:
/index.html
/compress.py
/download.py
EDIT:
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()
in this page now when i uploaded my application i get this output at this page(i believe that that this page-download.py-get the blob that has the specfic key in URL and download this file to my PC),but the result as:
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
without saving?
Thanks in advance.
Assuming you’re posting to
/compress.py, replacewith
That’ll get you to your next problem.
As an aside, you’ll probably find it easier to take smaller steps. A small step in this case would be “can I hit a handler via URL and at least get a ‘hello world’ result?”. Maybe you’ve already done that. The next small step is “Can I post to a handler and get a ‘hello world’ result?” Doing that without worrying at all (yet) about what to do with posted data removes a lot of possible problems from consideration. Take those problems on small step by small step.