如何在瓶子中设置wsgi.url_scheme为https?[英] How to set wsgi.url_scheme to https in bottle?

本文是小编为大家收集整理的关于如何在瓶子中设置wsgi.url_scheme为https?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想将所有请求重定向到http https.

在Python 2.7瓶应用程序中设置wsgi.url_scheme wsgi.url_scheme是否有通用方法?

应用程序的一般结构是:

setup.py  // contains 'install_requires'  
wsgi  
 - myapplication.py  // the custom application containing bottle routes

wsgi.url_scheme似乎与环境变量有关:

最新/definitys.html#envvar-wsgi.url_scheme

但我不确定如何将环境变量实际"设置"到https以及是否可以在setup.py或myapplication.py文件中进行.

这里有一个代码段:

https://github.com/defnull/bottle/bottle/issues/347 /p>

def i_am_https_dammit(app):
    def https_app(environ, start_response):
        environ['wsgi.url_scheme'] = 'https'
        return app(environ, start_response)
    return https_app

但我不知道如何实现这一点,因为我对应用程序的呼吁来自 cork and just:

application=default_app()  
session_opts = {
    'session.cookie_expires': True,
    'session.encrypt_key': 'please use a random key and keep it secret!',
    'session.httponly': True,
    'session.timeout': 3600 * 24,  # 1 day
    'session.type': 'cookie',
    'session.validate_key': True,
}
application = SessionMiddleware(application, session_opts)

推荐答案

如果我不理解您的问题,请原谅我,但是为什么不为您的瓶子应用安装简单的重定向插件呢?这样的东西:

import bottle

app = bottle.app()

def redirect_http_to_https(callback):
    '''Bottle plugin that redirects all http requests to https'''

    def wrapper(*args, **kwargs):
        scheme = bottle.request.urlparts[0]
        if scheme == 'http':
            # request is http; redirect to https
            bottle.redirect(bottle.request.url.replace('http', 'https', 1))
        else:
            # request is already https; okay to proceed
            return callback(*args, **kwargs)
    return wrapper

bottle.install(redirect_http_to_https)

@bottle.route('/hello')
def hello():
    return 'hello\n'

bottle.run(host='127.0.0.1', port=8080)

用卷曲测试:

% 05:57:03 !3000 ~>curl -v 'http://127.0.0.1:8080/hello'
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /hello HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 127.0.0.1:8080
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 303 See Other
< Date: Sun, 01 Dec 2013 10:57:16 GMT
< Server: WSGIServer/0.1 Python/2.7.5
< Content-Length: 0
< Location: https://127.0.0.1:8080/hello
< Content-Type: text/html; charset=UTF-8

有关插件工作方式的详细信息,请参阅瓶文档.

简而言之,该插件通过拦截所有请求并检查协议("方案")来起作用.如果该方案为" HTTP",则该插件指示瓶将HTTP重定向返回到相应的安全(https)URL.

本文地址:https://www.itbaoku.cn/post/889982.html

问题描述

I want to redirect all requests to http to https.

Is there a generic approach to setting wsgi.url_scheme to https in a Python 2.7 bottle application?

The general structure of the application is:

setup.py  // contains 'install_requires'  
wsgi  
 - myapplication.py  // the custom application containing bottle routes

wsgi.url_scheme seems to be related to environment variables:

http://wsgi.readthedocs.org/en/latest/definitions.html#envvar-wsgi.url_scheme

But I'm not sure how to actually 'set' the environment variable to https and whether it can be done in the setup.py or myapplication.py files.

There is a snippet of code here:

https://github.com/defnull/bottle/issues/347

def i_am_https_dammit(app):
    def https_app(environ, start_response):
        environ['wsgi.url_scheme'] = 'https'
        return app(environ, start_response)
    return https_app

But I don't know how I could implement the gist of this, as my call to the application is from cork and just uses:

application=default_app()  
session_opts = {
    'session.cookie_expires': True,
    'session.encrypt_key': 'please use a random key and keep it secret!',
    'session.httponly': True,
    'session.timeout': 3600 * 24,  # 1 day
    'session.type': 'cookie',
    'session.validate_key': True,
}
application = SessionMiddleware(application, session_opts)

推荐答案

Forgive me if I'm not understanding your question, but why not just install a simple redirect plugin for your Bottle app? Something like this:

import bottle

app = bottle.app()

def redirect_http_to_https(callback):
    '''Bottle plugin that redirects all http requests to https'''

    def wrapper(*args, **kwargs):
        scheme = bottle.request.urlparts[0]
        if scheme == 'http':
            # request is http; redirect to https
            bottle.redirect(bottle.request.url.replace('http', 'https', 1))
        else:
            # request is already https; okay to proceed
            return callback(*args, **kwargs)
    return wrapper

bottle.install(redirect_http_to_https)

@bottle.route('/hello')
def hello():
    return 'hello\n'

bottle.run(host='127.0.0.1', port=8080)

Tested with curl:

% 05:57:03 !3000 ~>curl -v 'http://127.0.0.1:8080/hello'
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /hello HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 127.0.0.1:8080
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 303 See Other
< Date: Sun, 01 Dec 2013 10:57:16 GMT
< Server: WSGIServer/0.1 Python/2.7.5
< Content-Length: 0
< Location: https://127.0.0.1:8080/hello
< Content-Type: text/html; charset=UTF-8

For details on how plugins work, see the Bottle docs.

Briefly, this plugin works by intercepting all requests and checking the protocol ("scheme"). If the scheme is "http", the plugin instructs Bottle to return an HTTP redirect to the corresponding secure (https) URL.