静态文件application_readable用法

jrs*_*rsm 9 python google-app-engine

我一直在试图理解application_readable静态url处理程序字段的工作原理.我正在使用SDK版本1.7.7并且我在我的开发环境中的应用程序上将其设置为true,但我似乎无法实际读取该文件:

# app.yaml

- url: /test
  static_dir: application/static/test
  application_readable: true

# app.py

path = os.path.join(os.path.split(__file__)[0], 'static/test/test.png')
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = '/application/static/test/test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = 'application/static/test/test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = '/static/test/test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = 'static/test/test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = '/test/test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = 'test/test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = '/test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
self.response.out.write("\n")
path = 'test.png'
self.response.out.write('Looking for %s...' % path)
self.response.out.write(os.path.exists(path))
Run Code Online (Sandbox Code Playgroud)

但这些都不起作用:

Looking for /vagrant/test/application/static/test/test.png...False
Looking for /application/static/test/test.png...False
Looking for application/static/test/test.png...False
Looking for /static/test/test.png...False
Looking for static/test/test.png...False
Looking for /test/test.png...False
Looking for test/test.png...False
Looking for /test.png...False
Looking for test.png...False
Run Code Online (Sandbox Code Playgroud)

虽然该文件肯定存在:

vagrant@precise64:/vagrant/kissyface$ ls -l /vagrant/test/application/static/test/test.png
-rwxrwxrwx 1 vagrant vagrant 9920 May  3 18:13 /vagrant/test/application/static/test/test.png
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我做错了什么?除了statis url处理程序文档中的简要描述以及appengine sdk 1.7.6 changelog中的提及之外,我还没有找到任何文档或示例代码.是否有一个实用程序类可以提供对这些文件的访问,或者我是否完全误解了application_readable实际上应该做什么?

Ezr*_*zra 16

概观

对你的系统究竟发生了什么事情进行推理有点困难,但我可以告诉你我的系统是什么.我们可以整天推测可能出现的问题,但实施有效和反向工作的东西通常比猜测更有成效; 它可能是任何东西.

如果我猜,我会说问题是:

  1. 处理程序顺序不正确
  2. 搞砸了python路径
  3. 搞砸python版本
  4. 使用一个janky旧SDK
  5. 内裤侏儒

如果你实现我在下面概述的项目而不是猜测,那么向后推理并找出它为什么不适合你的工作应该非常简单.如果这个项目不起作用,你还有一些工作要做.问题真的很讨厌(我不想帮你解决它).如果它确实有效,那么你很幸运,因为你还有5或10分钟的时间让代码的其余部分工作!

使用http://code.google.com/p/googleappengine/downloads/list中的最新python appengine SDK :

google_appengine_1.8.0.zip
71b5f3ee06dce0a7d6af32d65ae27272eac038cb
Run Code Online (Sandbox Code Playgroud)

项目文件及其内容:

目录设置:

.
??? app.py
??? app.pyc
??? app.yaml
??? static
    ??? hi.txt
Run Code Online (Sandbox Code Playgroud)

app.py:

import webapp2
import os

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!\n\n')

        path = os.path.join(os.path.split(__file__)[0], 'static/hi.txt')
        self.response.out.write(open(path).readlines()[0])

application = webapp2.WSGIApplication([('/.*', MainPage)])
Run Code Online (Sandbox Code Playgroud)

app.pyc是此文件的(自动)编译版本.

app.yaml中:

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /static
  static_dir: static
  application_readable: true
- url: /.*
  script: app.application
Run Code Online (Sandbox Code Playgroud)

静态/ hi.txt:

Ezra can see this text fine; I'm not sure why you can't... Hi!
Run Code Online (Sandbox Code Playgroud)

怎么了:

从项目根目录启动Web服务器:

 dev_appserver.py --port 80 .
Run Code Online (Sandbox Code Playgroud)

您可能必须使用不同的端口号; 这没什么大不了的.只需调整您选择的说明即可.

http://localhost/在浏览器中访问:

Http响应:

INFO     2013-05-14 09:45:57,372 server.py:585] default: "GET / HTTP/1.1" 200 85
Run Code Online (Sandbox Code Playgroud)

浏览器输出:

你好,webapp世界!

以斯拉可以看到这个文字很好; 我不确定你为什么不能......嗨!

http://localhost/static/hi.txt在浏览器中访问:

Http响应:

INFO     2013-05-14 09:48:42,785 server.py:585] default: "GET /static/hi.txt HTTP/1.1" 200 63
Run Code Online (Sandbox Code Playgroud)

浏览器输出:

以斯拉可以看到这个文字很好; 我不确定你为什么不能......嗨!

打破它:

如果我application_readable: true从app.yaml中删除该行:

http://localhost/在浏览器中访问:

Http响应:

ERROR    2013-05-14 09:51:13,290 webapp2.py:1528] [Errno 13] file not accessible: '.../static/hi.txt'
Run Code Online (Sandbox Code Playgroud)

浏览器输出:

500内部服务器错误

服务器出错或无法执行请求的操作.

接下来做什么:

希望你能从这个例子中倒退.如果它不适合你,你就注定要失败.享受一个阳光明媚的5月中旬下午在路上拖网,并试图(重新/不安装)安装东西以获得这个令人讨厌的东西.顶部的列表是我尝试的列表,不知道任何细节并排除了编程错误.祝好运!