简体   繁体   中英

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

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.

eg

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM