2

我在此链接http://ascii-chan-1018.appspot.com/上创建了一个名为 Ascii Art 的谷歌应用引擎应用程序,但由于某种原因,有时当您发布某些内容时,它会在屏幕上显示此错误代码“500 Internal Server错误 服务器出错或无法执行请求的操作。” 有时它可以工作,有时它会给你这个错误代码。我不确定它是我的源代码还是谷歌服务器上的错误。

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)])
4

1 回答 1

0

好吧,我不确定您是否可以考虑解决问题,但我刚刚删除了剩下的谷歌地图代码。它一直抱怨除了 URLError 之外的行,所以在我删除地图代码后它工作正常,我最初的计划是无论如何都要删除它。

于 2015-07-31T14:30:35.357 回答