Google App Engine, как получить часть из URL в запросе на получение? - PullRequest
0 голосов
/ 04 ноября 2010

мой URL-адрес: "http://localhost:8080/i/agt0b3R0eXN3b3JsZHIQCxIJSW1hZ2VCbG9iGIUDDA.jpg", и мне просто нужна часть" agt0b3R0eXN3b3JsZHIQCxIJSW1hZ2VCbG9iGIUDDA ".

мой app.yaml выглядит так:

handlers:
- url: /i/.*
  script: static_images.py

statc_images

class StaticImage(webapp.RequestHandler):
    def get(self):
        image_blob_key = db.Key(self.request.get('')) # here I need the blob_key from url, in this case is "agt0b3R0eXN3b3JsZHIQCxIJSW1hZ2VCbG9iGIUDDA"

        image_blob = db.get(image_blob_key)

        if image_blob:
            self.response.headers['Content-Type'] = 'image/jpeg'
            self.response.out.write(image_blob.data)
        else:
            self.response.out.write("Image not available")

def main():
    app = webapp.WSGIApplication([('/i/(\d+)\.jpg', StaticImage)], debug=True) # im not pretty sure this is good: '/i/(\d+)\.jpg'
    run_wsgi_app(app)

if __name__ == "__main__":
    main()

спасибо большое!;)

1 Ответ

3 голосов
/ 04 ноября 2010

Я думаю, вы довольно близки. Попробуйте это:

class StaticImage(webapp.RequestHandler):
    def get(self, blob_key):
        image_blob = ImageModel.get(blob_key)
        # if you want to use db.get you could.

        if image_blob:
            self.response.headers['Content-Type'] = 'image/jpeg'
            self.response.out.write(image_blob.data)
        else:
            self.response.out.write("Image not available")

def main():
    app = webapp.WSGIApplication([('/i/(.*)\.jpg', StaticImage)], debug=True)
    run_wsgi_app(app)

if __name__ == "__main__":
    main()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...