问题
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