如何根据请求的URL的域名在Flask中路由? [英] How do I route in Flask based on the domain of the requested URL?

查看:277
本文介绍了如何根据请求的URL的域名在Flask中路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



根据我读过的内容<这里和其他地方,看起来像这样应该可以用类似于

  Flask 
application = Flask(__ name__)

application.url_map.host_matching = True

@ application.route(/,host =< prefix> .mydomain.com:< port>)
def mydomain(prefix ='',port = 0,** kwargs):
return'This is My Domain:with prefix'+ prefix

@ application.route(/,host =< prefix>。< project> elasticbeanstalk.com:< port>)
def test(prefix ='',project ='',port = 0,** kwargs):
return'您正在阅读EB'+项目

@ application.route(/,h )
def catchall(** kwargs):
return'这是什么'

但是404失败,找不到页面。还有什么我需要做的,以得到这个工作?链接的所以答案提到当你设置host_matching为true时需要为所有路由指定主机,但是我不知道这是什么意思,或者它会是什么样子(我认为这就是我上面做的)。

如何在Flask中根据请求的URL?






重要的是,此站点位于AWS Elastic Beanstalk上。调试这些情况的一种方法是跳到一个控制台,并使用底层函数:


pre> >>> from werkzeug.routing import Map,Rule
>>> m = Map([Rule('/',endpoint ='endpoint',host ='< host> .example.com:< port>)],host_matching = True)
>>> ; c = m.bind('open.example.com:888')
>>> '''')
('endpoint', pre>

如果不匹配,则会引发NotFound异常。



您可以在同一行上运行该命令

 >> ;>地图([Rule('/',endpoint ='endpoint',host ='< host> .example.com:< port>)],host_matching = True).bind('open.example.com:888 ').match('/')

获得一个真正快速的反馈循环错误。我从你的代码示例中唯一不能告诉的是实际的主机字符串是什么样子,这是重要的一部分。这就是你需要知道的东西,并把它放到 m.bind 调用中。所以,如果你能告诉我什么主机字符串在你的特定情况下,我可以肯定地调试这个给你。


$ b 您提供的示例主机字符串是:www.myproject-elasticbeanstalk.com。

 >>> Map([Rule('/',endpoint ='endpoint',host ='< prefix>。< project> -elasticbeanstalk.com')],host_matching = True).bind('www.myproject-elasticbeanstalk.com ').match('/')
('endpoint',{'prefix':u'www','project':u'myproject'})

因此,修改后的主字符串'< prefix>。< project> -elasticbeanstalk.com'匹配,并将前缀和项目传递到视图。也许只是当主机字符串不包含它们时,你试图匹配端口号?

I'm trying to implement routing based on the host of the requested URL for a Flask site I'm building.

Based on what I've read here and elsewhere, it seems like this should be possible with something like

from flask import Flask
application = Flask(__name__)

application.url_map.host_matching = True

@application.route("/", host="<prefix>.mydomain.com:<port>")
def mydomain(prefix='', port=0, **kwargs):
    return 'This is My Domain: with prefix  ' + prefix

@application.route("/", host="<prefix>.<project>elasticbeanstalk.com:<port>")
def test(prefix='', project='', port=0, **kwargs):
    return 'You are reading from EB  ' + project

@application.route("/", host="<host>:<port>")
def catchall(**kwargs):
    return 'This is anything'

but this fails with 404 "page not found". Is there something else I need to do to get this working? The linked SO answer mentions "the need to specify the host for all routes when you set host_matching to true" but I'm not sure what this means or what it would look like (I thought that's what I had been doing above).

How do I route in Flask based on the domain of the requested URL?


If it matters, this site is hosted on AWS Elastic Beanstalk.

解决方案

One way of debugging these situations is to just jump into a console and play with the underlying functions:

>>> from werkzeug.routing import Map, Rule
>>> m = Map([Rule('/', endpoint='endpoint', host='<host>.example.com:<port>')], host_matching=True)
>>> c = m.bind('open.example.com:888')
>>> c.match('/')
('endpoint', {'host': u'open', 'port': u'888'})

If it didn't match it would raise a NotFound exception.

You can run that command on one line

>>> Map([Rule('/', endpoint='endpoint', host='<host>.example.com:<port>')], host_matching=True).bind('open.example.com:888').match('/')

And get a really quick feedback loop about what you are doing wrong. The only thing I can't tell from your code example is what the actual host string looks like... and that's the important part. That's what you need to know and feed into the m.bind call. So if you can tell me what the host string looks like in your particular situation I can definitely debug this for you.

An example host string provided by you was: www.myproject-elasticbeanstalk.com.

>>> Map([Rule('/', endpoint='endpoint', host='<prefix>.<project>-elasticbeanstalk.com')], host_matching=True).bind('www.myproject-elasticbeanstalk.com').match('/')
('endpoint', {'prefix': u'www', 'project': u'myproject'})

So a modified host string of '<prefix>.<project>-elasticbeanstalk.com' matches that and would pass the prefix and project into the view. Maybe it's simply that you're trying to match on port numbers when the host strings don't include them?

这篇关于如何根据请求的URL的域名在Flask中路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆