如何接受Python瓶子服务器中的URL?

2024-05-13 10:33:08 发布

您现在位置:Python中文网/ 问答频道 /正文

使用瓶子Sehttp://bottlepy.org/docs/dev/routing.html#通配符过滤器

我想接受任何url,然后对该url执行操作。

例如

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

这很棘手,因为url中有斜线,瓶子被斜线分开。


Tags: pathorgdevurl过滤器docs瓶子bottle
3条回答

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

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

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

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

我想你一开始就走对了。<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

这是我的密码。当你在你的系统上运行时会发生什么?

from bottle import route, run

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

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

干杯!

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

应该工作得很好

然后我将把regex写入函数本身。

或者你可以用一个新的过滤器,但是要做到这一点,你必须编写一个过滤器函数并将其添加到应用程序中。

相关问题 更多 >