How do you accept any URL in a Python Bottle server?

自闭症网瘾萝莉.ら 提交于 2019-11-30 17:53:45

Based on new Bottle (v0.10), use a re filter:

@bottle.route("/<url:re:.+>")

You can do that with old parameters too:

@bottle.route("/:url#.+#")

I think you (OP) were on the right track to begin with. <mypath:path> should do the trick.

I just tried it out with bottle 0.10 and it works:

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

Here's my code. What happens when you run this on your system?

from bottle import route, run

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

run(host='localhost', port=8090)

Cheers!

@bottle.route("/hello/:myurl")
def something(myurl):
    print myurl
    return "Your url was %s" % myurl

Should work just fine

I would then write the regex into the function itself.

Or you could do it with a new filter, but to do that you have to write a filter function and add it to the app.

In Bottle 0.12.9 i did this to achieve optional dynamic routes:

@bottle.route("/<url:re:.*>")
def index(url):
  return "Your url is " + url
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!