2010-11-04 107 views
0

我的網址是:「http:// localhost:8080/i/agt0b3R0eXN3b3JsZHIQCxIJSW1hZ2VCbG9iGIUDDA.jpg」,我只想要「agt0b3R0eXN3b3JsZHIQCxIJSW1hZ2VCbG9iGIUDDA」部分。Google應用引擎如何在獲取請求中獲取url的一部分?

我app.yaml的樣子:

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

statc_images.py:

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

屬於在http://www.stackoverflow.com ... – kafuchau 2010-11-04 16:50:36

回答

3

我覺得你很親密。試試這個:

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() 
+2

您可能要列出的差異,爲了清楚:圓括號的URL正則表達式的相關部分,並且參數添加到get方法接受子表達式的值。 – 2010-11-04 23:43:47