Skip to main content
 首页 » 编程设计

python之Bottle.py 的动态路由

2025年05月04日8dyllove98

我是 Bottle 框架的新手,在尝试使用动态路由提供静态文件时,我很难理解我做错了什么。

当我使用精确值时,以下内容对我来说效果很好:

@route('/files/somefile.txt') 
def serve_somefile(): 
   return static_file('somefile.txt', root = '/directory/to/files') 

但是,我正在尝试创建一个动态路由来根据 documentation 为/files 目录中的任何文件提供服务。 .

这对我不起作用:

@route('/files/<filename>') 
def serve_somefile(filename): 
   return static_file(filename, root= '/directory/to/files') 

尽管与上述示例相比,它收到了相同的 GET 请求,但我从服务器收到了 404 响应。

谁能指出我在这里做错了什么?

请您参考如下方法:

您是否尝试将参数指定为路径(就像在他们的示例中一样):

@route('/files/<filename:path>') 
def serve_somefile(filename): 
    return static_file(filename, root= '/directory/to/files')