我有一个应用程序,工作良好。我试图使一个脚本只运行,如果用户是管理员(我)。管理员登录工作正常,但后来我得到重定向到页面/地理代码,什么也没发生。我看到一个HTTP响应为200。
有什么建议吗?
感谢EDIT 2:根据要求添加完整的代码。
app.yaml:
application: theapp
version: 1
runtime: python
api_version: 1
handlers:
- url: /geocode
script: geocode.py
login: admin
- url: /admin/.*
script: $PYTHON_LIB/google/appengine/ext/admin
login: admin
- url: /favicon.ico
static_files: static/images/favicon.ico
upload: static/images/favicon.ico
- url: /static
static_dir: static
- url: /.*
script: first.py
secure: never
builtins:
- appstats: on
inbound_services:
- warmup
first.py:
import os
from beer import Beer
from desktop import Desktop
from geocode import Geocode
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
application = webapp.WSGIApplication(
[('/', Desktop),
('/beer', Beer),
('/geocode', Geocode)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
geocode.py:
import pubs
import os
import time
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from google.appengine.api import urlfetch
import urllib
import urllib2
import simplejson
class Geocode(webapp.RequestHandler):
'''Class for ./geocode
A couple of methods to take the address' from the Pubs.pub_address array
and turn them into usefull Latitudes & Longitudes.
'''
def get_lat_long(self, location):
place = urllib.quote_plus(location)
print location
url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=true®ion=no" % (place)
print url
out = urllib.urlopen(url)
jsonResponse = simplejson.load(out)
pub_location = jsonResponse.get('results')[0].get('geometry').get('location')
lat = pub_location.get('lat')
lng = pub_location.get('lng')
return lat, lng
def get(self):
pub_address = []
pub_bounce = []
self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
path = os.path.join(os.path.dirname(__file__), 'templates')
path = os.path.join(path, 'geocode.html')
for i, element in enumerate(pubs.pub_list):
pub_bounce.append(pubs.pub_list[i].pub_bounce)
for i, element in enumerate(pubs.pub_list):
pub_address.append(pubs.pub_list[i].pub_address.encode('utf-8'))
for i, element in enumerate(pub_address):
pubs.pub_list[i].pub_lat, pubs.pub_list[i].pub_lng = self.get_lat_long(pub_address[i])
print pubs.pub_list[i].pub_lat, pubs.pub_list[i].pub_lng
time.sleep(.2)
template_values = {"pub_address": pub_address, "pub_bounce": pub_bounce}
self.response.out.write(template.render(path, template_values))
我想你需要这个:
- url: /geocode
script: first.py
login: admin
从app.yaml引用的脚本需要是一个应用程序处理程序(例如在其中有run_wsgi_app等)。Geocode.py不是一个应用程序处理程序——它是first.py导入的一些代码。
您的geocode.py通过POST方法传递变量吗?
根据我的经验,当重定向到"login"页面时,通过POST方法传递的变量将丢失。(通过GET传递的变量将保留)那么就会出现这样的情况:响应200,但实际上什么也没发生。
# app.yaml application: xxx version: 1 runtime: python api_version: 1 handlers: - url: /test.html static_files: test.html upload: test.html - url: / login: admin script: urls.py
# urls.py
from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import logging
class Test(webapp.RequestHandler):
def get(self):
print "get"
print self.request.get("var")
logging.info("GET %s"%self.request.get("var"))
def post(self):
print "post"
print self.request.get("var")
logging.info("POST %s"%self.request.get("var"))
application = webapp.WSGIApplication([
(r'/', Test),])
def main():
run_wsgi_app(application)
#===============================================================================
# # it is necessary; otherwise, it will cause the server won't response
# # at the first request which create a instance
#===============================================================================
if __name__ == "__main__":
main()
<!-- test.html -->
<html>
<body>
<form method="POST" action="/">
<input type="text" name="var" value="123">
<input type="submit">
</form>
</body>
</html>