Jos*_*ton 14 python google-app-engine redirect
如何阻止用户访问example.appspot.com上的应用并强制他们在example.com上访问它?我已经有example.com工作,但我不希望用户能够访问appspot域.我正在使用python.
Ale*_*lli 17
您可以检查os.environ['HTTP_HOST'].endswith('.appspot.com')- 如果是- 那么您正在服务something.appspot.com并且可以发送重定向,或者根据需要改变您的行为.
您可以以各种方式(装饰器,WSGI中间件,从您的中间基类继承子类webapp.RequestHandler[[或任何其他基本处理程序] )部署此检查和重定向(如果需要)(或您选择的其他行为更改)你正在使用的类]]和方法名称与你的应用程序级处理程序类中的get和post不同,但我认为这里的关键思想os.environ是由app引擎框架根据CGI标准设置的因此,您可以依赖这些标准(类似地,WSGI基于它从os.environ中获取的值构建自己的环境).
上面发布的代码有两个问题 - 它尝试重定向安全流量(自定义域不支持),当Google在您的appspot域上调用它们并且您提供301时,您的cron作业也会失败.
我在我的博客上发布了一个略微修改过的版本:http: //blog.dantup.com/2009/12/redirecting-requests-from-appid-appspot-com-to-a-custom-domain
为方便起见,我已经包含了以下代码.
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
def run_app(url_mapping):
application = webapp.WSGIApplication(url_mapping, debug=True)
application = redirect_from_appspot(application)
run_wsgi_app(application)
def redirect_from_appspot(wsgi_app):
"""Handle redirect to my domain if called from appspot (and not SSL)"""
from_server = "dantup-blog.appspot.com"
to_server = "blog.dantup.com"
def redirect_if_needed(env, start_response):
# If we're calling on the appspot address, and we're not SSL (SSL only works on appspot)
if env["HTTP_HOST"].endswith(from_server) and env["HTTPS"] == "off":
# Parse the URL
import webob, urlparse
request = webob.Request(env)
scheme, netloc, path, query, fragment = urlparse.urlsplit(request.url)
url = urlparse.urlunsplit([scheme, to_server, path, query, fragment])
# Exclude /admin calls, since they're used by Cron, TaskQueues and will fail if they return a redirect
if not path.startswith('/admin'):
# Send redirect
start_response("301 Moved Permanently", [("Location", url)])
return ["301 Moved Peramanently", "Click Here %s" % url]
# Else, we return normally
return wsgi_app(env, start_response)
return redirect_if_needed
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4430 次 |
| 最近记录: |