google 应用引擎不断出现 python 500 错误

google app engine with python 500 error keeps coming

我在 link http://ascii-chan-1018.appspot.com/ 创建了一个名为 Ascii Art 的 google 应用程序引擎应用程序,但由于某些原因,有时当你 post 某些东西时它会给你这个错误屏幕上的代码 “500 Internal Server Error 服务器出错或无法执行请求的操作。”有时它有效,有时它会给你这个错误代码。我不确定它是我的源代码还是 google 的服务器上的错误。

import os     
import re
import sys
import urllib2
import random
import logging
from xml.dom import minidom
from string import letters
import webapp2
import jinja2
from google.appengine.api import memcache
from google.appengine.ext import db

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)

art_key = db.Key.from_path('ASCIIChan', 'arts')

def console(s):
        sys.stderr.write('%s\n' % s)

IP_URL = "http://api.hostip.info/?ip="
def get_coords(ip):
        ip = "17.173.254.223"
        url = IP_URL + ip
        content = None
        try:
                content = urllib2.urlopen(url).read()
        except URLError:
                return

        if content:
                d = minidom.parseString(content)
                coords = d.getElementsByTagName("gml:coordinates")
                if coords and coords[0].childNodes[0].nodeValue:
                        lon, lat = coords[0].childNodes[0].nodeValue.split(',')
                        return db.GeoPt(lat, lon)

class Handler(webapp2.RequestHandler):
        def write(self, *a, **kw):
                self.response.out.write(*a, **kw)

        def render_str(self, template, **params):
                t = jinja_env.get_template(template)
                return t.render(params)

        def render(self, template, **kw):
                self.write(self.render_str(template, **kw))

GMAPS_URL = "http://maps.googleapis.com/maps/api/staticmap?size=380x263&sensor=false&"
def gmap_img(points):
        markers = '&'.join('markers=%s,%s' % (p.lat, p.lon) for p in points)
        return GMAPS_URL + markers

class Art(db.Model):
        title = db.StringProperty(required = True)
        art = db.TextProperty(required = True)
        created = db.DateTimeProperty(auto_now_add = True)
        coords = db.GeoPtProperty( )

def top_arts(update = False):
        key = 'top'
        arts = memcache.get(key)
        if arts is None or update:
                logging.error("DB QUERY")
                arts = db.GqlQuery("SELECT * "
                                        "FROM Art "
                                        "WHERE ANCESTOR IS :1 "
                                        "ORDER BY created DESC "
                                        "LIMIT 10",
                                        art_key)
                arts = list(arts)
                memcache.set(key, arts)
        return arts

class MainPage(Handler):
        def render_front(self, title="", art="", error=""):
                arts = top_arts()

                img_url = None
                points = filter(None, (a.coords for a in arts))
                if points:
                        img_url = gmap_img(points)

                #display the image URL
                self.render("Ascii.html", title = title, art = art, error = error, arts = arts, img_url = img_url)

        def get(self):
                self.render_front()

        def post(self):
                title = self.request.get("title")
                art = self.request.get("art")

                if title and art:
                        p = Art(parent=art_key, title = title, art = art)
                        #lookup the user's coordinates from their IP
                        coords = get_coords(self.request.remote_addr)
                        #if we have coordinates, add them to the art
                        if coords:
                                p.coords = coords
                        p.put()
                        #rerun the query and update the cache
                        top_arts(True)

                        self.redirect("/")
                else:
                        error = "Invalid, are you sure you entered a title and art work?"
                        self.render_front(error = error, title = title, art =art)

app = webapp2.WSGIApplication([('/', MainPage)])

嗯,我不确定您是否可以考虑用它来解决问题,但我只是删除了遗留下来的 google 地图代码。它一直抱怨除 URLError 之外的行,所以在我删除地图代码后它工作正常,我最初的计划是无论如何都将其删除。