我正在尝试在GAE中部署以下名为image-getter.py的python代码:
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext import os
from google.appengine.ext.webapp.util import run_wsgi_app
#the addimage endpoint
class AddImage(webapp.RequestHandler):
def post(self):
image = self.request.get('image')
i = Image()
i.picture = db.Blob(image)
i.put()
self.response.out.write('done');
#the Image object:
class Image(db.Model):
picture = db.BlobProperty();
#to get the image : /getimage?key=sdfsadfsf...
class GetImage(webapp.RequestHandler):
def get(self):
images_query = Image.get(self.request.get('key'))
if (images_query and images_query.picture):
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(images_query.picture)
#to draw the images out to the main page:
class MainPage(webapp.RequestHandler):
def get(self):
images = db.Query(Image)
keys = [];
for image in images:
keys.append(str(image.key()))
template_values = {'images' : keys}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
def main():
app = webapp.WSGIApplication(
[('/', MainPage),
], debug=True)
上面的代码使用了os库,但我认为你不允许在GAE中使用它。
我的app.yaml文件如下:
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
script: image-getter.app
libraries:
html,index.html文件如下所示:
<div>
{% for i in images %}
<img src="/getimage?key={{i}}" />
{% endfor %}
</div>
我似乎无法让应用程序运行,我得到“错误:服务器错误”,这不是非常有用。
感谢!
答案 0 :(得分:2)
image-getter.py中没有image-getter.app。此外,您的image-getter.py检查示例中没有路由https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld
您需要添加类似
的内容app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
当您发布代码时,请包含import语句,您的代码似乎无效,因为它不会导入db模块。