Static files application_readable usage

2019-06-16 05:34发布

I've been trying to understand how the application_readable static url handler field works. I'm using SDK version 1.7.7 and I've set this to true on an application in my dev environment, but I can't seem to actually read the file:

# 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))

But none of those work:

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

Though the file definitely exists:

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

Can anyone tell me what I'm doing wrong? I haven't been able to find any documentation or example code for this beyond the brief description in the statis url handler docs and a mention in the appengine sdk 1.7.6 changelog. Is there a utility class that provides access to these files, or am I completely misinterpreting what application_readable is actually supposed to do?

1条回答
Emotional °昔
2楼-- · 2019-06-16 06:07

Overview

It's somewhat difficult to reason about what exactly is going on on your system, but I can tell you what works on mine. We can speculate all day about what could be wrong, but implementing something that works and working backwards is usually more productive than guessing; it could be anything.

If I were to guess, I'd say that the problem is:

  1. Incorrect handler order
  2. Screwed up python paths
  3. Screwed up python versions
  4. Using a janky old SDK
  5. Underpants gnomes

If, instead of guessing, you implement the project I've outlined below, it should be pretty simple to reason backward and find out why it's not working for you. If this project doesn't work, you've got some work to do. The problem's really nasty (and I don't want to help you fix it). If it does work, you're in luck, since you're 5 or 10 minutes away from having the rest of your code working too!

Using the latest python appengine SDK from http://code.google.com/p/googleappengine/downloads/list:

google_appengine_1.8.0.zip
71b5f3ee06dce0a7d6af32d65ae27272eac038cb

Project files and their contents:

Directory Setup:

.
├── app.py
├── app.pyc
├── app.yaml
└── static
    └── hi.txt

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)])

app.pyc is the (automatically) compiled version of this file.

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

static/hi.txt:

Ezra can see this text fine; I'm not sure why you can't... Hi!

What happens:

Start the webserver from the project root:

 dev_appserver.py --port 80 .

You might have to use a different port number; it's no big deal. Just adjust the instructions that follow for the one you choose.

Visiting http://localhost/ in the browser:

Http response:

INFO     2013-05-14 09:45:57,372 server.py:585] default: "GET / HTTP/1.1" 200 85

Browser output:

Hello, webapp World!

Ezra can see this text fine; I'm not sure why you can't... Hi!

Visiting http://localhost/static/hi.txt in the browser:

Http response:

INFO     2013-05-14 09:48:42,785 server.py:585] default: "GET /static/hi.txt HTTP/1.1" 200 63

Browser output:

Ezra can see this text fine; I'm not sure why you can't... Hi!

Breaking it:

If I remove the application_readable: true line from app.yaml:

Visiting http://localhost/ in the browser:

Http Response:

ERROR    2013-05-14 09:51:13,290 webapp2.py:1528] [Errno 13] file not accessible: '.../static/hi.txt'

Browser output:

500 Internal Server Error

The server has either erred or is incapable of performing the requested operation.

What to do next:

Hopefully you can work backwards from this example. If it doesn't work for you, you're doomed. Enjoy spending a sunny mid-May afternoon trawling through paths and trying to (re/un)install things to get this frickin' thing going. The list at the top is a list of I'd try, without knowing any specifics and having ruled out programming error. Good luck!

查看更多
登录 后发表回答