你如何接受Python Bottle服务器中的任何URL?

Dav*_*ave 13 python bottle

使用瓶子Sehttp://bottlepy.org/docs/dev/routing.html#wildcard-filters

我想接受任何网址,然后用网址做点什么.

例如

@bottle.route("/<url:path>")
def index(url):
  return "Your url is " + url
Run Code Online (Sandbox Code Playgroud)

这很棘手,因为URL中有斜杠,而Bottle则用斜杠分割.

iur*_*vio 16

基于新瓶(v0.10),使用重新过滤器:

@bottle.route("/<url:re:.+>")
Run Code Online (Sandbox Code Playgroud)

您也可以使用旧参数执行此操作:

@bottle.route("/:url#.+#")
Run Code Online (Sandbox Code Playgroud)


ron*_*man 9

我认为你(OP)开始走上正轨. <mypath:path>应该做的伎俩.

我只是用瓶子0.10尝试了它,它的工作原理:

~>python test.py >& /dev/null &
[1] 37316
~>wget -qO- 'http://127.0.0.1:8090/hello/cruel/world'
Your path is: /hello/cruel/world
Run Code Online (Sandbox Code Playgroud)

这是我的代码.在系统上运行此操作会发生什么?

from bottle import route, run

@route('<mypath:path>')
def test(mypath):
    return 'Your path is: %s\n' % mypath

run(host='localhost', port=8090)
Run Code Online (Sandbox Code Playgroud)

干杯!