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

杀马特。学长 韩版系。学妹 提交于 2019-11-30 02:32:46

问题


Using a Bottle Sehttp://bottlepy.org/docs/dev/routing.html#wildcard-filters

I'd like to accept any url, and then do something with the url.

e.g.

@bottle.route("/<url:path>")
def index(url):
  return "Your url is " + url

This is tricky because URLs have slashes in them, and Bottle splits by slashes.


回答1:


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#.+#")



回答2:


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!




回答3:


@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.




回答4:


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


来源:https://stackoverflow.com/questions/8171618/how-do-you-accept-any-url-in-a-python-bottle-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!