0

webapp2 を使用して簡単なアプリを作成したいと考えていました。Google App Engine がインストールされていて、GAE の外部で使用したいので、次のページの指示に従いました: http://webapp-improved.appspot.com/tutorials/quickstart.nogae.html

これはすべてうまくいきました。私の main.py は実行されており、リクエストを正しく処理しています。ただし、リソースに直接アクセスすることはできません。

http://localhost:8080/myimage.jpgまたhttp://localhost:8080/mydata.json

常に 404 リソースが見つからないページを返します。リソースを WebServer/Documents/ に配置するか、virtualenv がアクティブなフォルダーに配置するかは問題ではありません。

助けてください!:-)

(私はPython 2.7を搭載したMac 10.6を使用しています)

4

1 に答える 1

2

(この質問から適応)

webapp2 には静的ファイル ハンドラーがないようです。自分で巻く必要があります。ここに簡単なものがあります:

import mimetypes

class StaticFileHandler(webapp2.RequestHandler):
    def get(self, path):
        # edit the next line to change the static files directory
        abs_path = os.path.join(os.path.dirname(__file__), path)
        try:
            f = open(abs_path, 'r')
            self.response.headers.add_header('Content-Type', mimetypes.guess_type(abs_path)[0])
            self.response.out.write(f.read())
            f.close()
        except IOError: # file doesn't exist
            self.response.set_status(404)

appオブジェクトに、次のルートを追加しますStaticFileHandler

app = webapp2.WSGIApplication([('/', MainHandler), # or whatever it's called
                               (r'/static/(.+)', StaticFileHandler), # add this
                               # other routes
                              ])

http://localhost:8080/static/mydata.json(言う)がロードされますmydata.json

このコードは潜在的なセキュリティ リスクであることに注意してください。これにより、Web サイトへのすべての訪問者が静的ディレクトリ内のすべてを読み取ることができます。このため、すべての静的ファイルは、アクセスを制限したいもの (ソース コードなど) を含まないディレクトリに保存する必要があります。

于 2013-05-12T14:53:24.160 に答える