如何在python falcon中进行内部重定向

时间:2018-03-15 00:41:19

标签: falconframework

如何在Falcon中进行“内部重定向”?

我使用以下方式设置静态路由:

app.add_static_route('/', os.path.abspath(here + '/static/')

我想将' / '重定向到' /index.html ',但不是作为http 3xx,我想在内部进行,以便关于浏览器的路径仍然是' / ',但内容是' /static/index.html '的内容。

1 个答案:

答案 0 :(得分:0)

从Falcon 1.4.1开始,没有办法做“内部重定向”而不回复单个文件,但是我能够使用接收器和正则表达式实现这一点:

import os
import falcon

WORKING_DIRECTORY = os.getcwd()
STATIC = 'static/'

def apex(req, resp):
    resp.content_type = 'text/html; charset=utf-8'
    filename = os.path.abspath(os.path.join(WORKING_DIRECTORY, STATIC, 'index.html'))
    with open(filename, 'rt') as f:
        resp.body = f.read()

app.add_sink(apex, prefix='^/$')
app.add_static_route('/', os.path.abspath(os.path.join(WORKING_DIRECTORY, STATIC)))