将appspot.com域名重定向到Google Apps域名
2009 9 6 03:11 PM 3182次查看
分类:Google App Engine 标签:Google App Engine, Python
先看看把本地服务器上,将127.0.0.1重定向到localhost的方法:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
def redirect_from_appspot(wsgi_app):
from_server = '127.0.0.1'
to_server = 'localhost'
def redirect_if_needed(env, start_response):
if env['HTTP_HOST'].startswith(from_server):
import webob, urlparse
request = webob.Request(env)
scheme, netloc, path, query, fragment = urlparse.urlsplit(request.url)
url = urlparse.urlunsplit([scheme, netloc.replace(from_server, to_server, 1), path, query, fragment]) # 使用replace是为了不丢失端口信息
start_response('301 Moved Permanently', [('Location', url)])
return [] # 这个函数必须返回一个可迭代对象
else:
return wsgi_app(env, start_response)
return redirect_if_needed
class Main(webapp.RequestHandler):
def get(self):
self.response.out.write('hello')
application = webapp.WSGIApplication([('.*', Main)])
def main():
util.run_wsgi_app(redirect_from_appspot(application))
if __name__ == '__main__':
main()
再来看GAE服务器上如何将http重定向到自己的域名:def redirect_from_appspot(wsgi_app):
from_server = 'keakon.appspot.com'
to_server = 'gae.keakon.cn'
def redirect_if_needed(env, start_response):
if env['HTTP_HOST'].startswith(from_server) and env['HTTPS'] == 'off': # HTTPS只有appspot.com的域名支持,因此不重定向
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])
start_response('301 Moved Permanently', [('Location', url)])
return []
else:
return wsgi_app(env, start_response)
return redirect_if_needed
向下滚动可载入更多评论,或者点这里禁止自动加载。