【问题标题】:Call ServeHandler from main function从主函数调用 ServeHandler
【发布时间】:2012-04-12 07:36:29
【问题描述】:

我正在尝试通过 URL 将参数从 python 页面传递到另一个页面,这个参数(我的问题中的关键)被正确传递但在另一个页面中我有这个代码:

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
import urllib2        
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self):

          blob_key = self.request.get('key')
          blob_key = str(urllib.unquote(blob_key))
          blob_info = blobstore.BlobInfo.get(blob_key)
          self.send_blob(blob_info)

def main():

     application = webapp.WSGIApplication(
    [('/',ServeHandler),], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
    main()

但是类ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):没有执行的问题,当我运行这段代码时,输​​出是

Status: 404 Not Found
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 0

虽然参数传递正确;这是url和key(参数):

http://localhost:8080/download.py?key=vzsX4xM1EtNak5RQVxj4BQ==

编辑: 这是我的 app.yaml 代码:

application: myapplication
version: 1
runtime: python
api_version: 1

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

我想要这个问题的解决方案吗?欢迎提出任何建议。

【问题讨论】:

  • 你的app.yaml是什么样的?
  • 亲爱的上帝,伙计;请将该信息添加到您的问题中(编辑)
  • @bernie:好的,我会编辑问题。

标签: python google-app-engine blobstore python-2.5


【解决方案1】:

/download.py 甚至不在您的app.yaml 中。
webapp 如何找到正确的脚本来运行?

现在专注于下载脚本,这是您的 app.yaml 的外观示例:

处理程序: - 网址:/.* 脚本:下载.py

编辑:
或者,您可以为下载 URL 指定更具体的正则表达式模式:

def main():
    application = webapp.WSGIApplication([
            (r'/download.*', ServeHandler),
        ], debug=True)

URL 应该是这样的:

http://localhost:8080/download?key=vzsX4xM1EtNak5RQVxj4BQ==

【讨论】:

  • :谢谢,但是如果我把它写在 app.yaml 中,那么主页面将作为(download.py)页面执行,我的项目有 3 个 python 页面和一个 html 文件作为上面显示在 app.yaml 中?
  • 一个有效的问题。我会更新答案。但这需要您多做一步。坚持住。
  • :非常感谢您的帮助,现在上课了。
猜你喜欢
  • 2020-12-18
  • 2015-10-09
  • 2016-05-27
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
相关资源
最近更新 更多