Skip to content Skip to sidebar Skip to footer

How To Use Wsgi To Reroute A User From Http To Https

Original Question App Engine SDK 1.6.5 Python 2.7 webapp2 I have implemented webapp2 schemes to secure pages to https. The issue is that when a user goes to say http:// site/lo

Solution 1:

Set the URLs in app.yaml, rather than in code. See https://developers.google.com/appengine/docs/python/config/appconfig#Secure_URLs

For example:

handlers:-url:/foo/.*script:accounts.pysecure:always

This will redirect HTTP to HTTPS.

Solution 2:

This is the working code I used in testing for this question.

Note: The development Web Server (as of this writing v1.6.5) doesn't support https so your WSGI routes will need the schemes removed to work in the development environment. You can add them back before deployment or create an variable to set the scheme that checks the environment as I did below.

You can get App Engine Python to reroute the request by defining app.yaml as: app.yaml

application:cgi-vs-wsgiversion:1runtime:python27api_version:1threadsafe:yeslibraries:-name:webapp2version:latesthandlers:-url:/profilescript:main.appsecure:always-url:/loginscript:main.appsecure:always-url:/.*script:main.app

Then in main.py you can declare your WSGI Handlers as normal like:

main.py

import webapp2
import os

# Modelsfrom models.Shout import Shout

# Controllersfrom controllers.HomeHandler import HomeHandler
from controllers.LoginHandler import LoginHandler
from controllers.ProfileHandler import ProfileHandler

if os.environ['SERVER_SOFTWARE'].startswith('Development'):
    app_scheme = 'http'else:
    app_scheme = 'https'

app = webapp.WSGIApplication([
    webapp2.Route(r'/login', LoginHandler, name='login', schemes=[app_scheme]),
    webapp2.Route(r'/profile', ProfileHandler, name='profile', schemes=[app_scheme]),
    webapp2.Route(r'/', HomeHandler)
], debug=True)

I have uploaded the code for this app to my AE-BaseApp GitHub please feel free to download and use this in your applications. The code is licensed Apache License 2.0.

Post a Comment for "How To Use Wsgi To Reroute A User From Http To Https"