单个 Pyramid 实例上的多个域和子域 [英] Multiple domains and subdomains on a single Pyramid instance

查看:38
本文介绍了单个 Pyramid 实例上的多个域和子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在单个 Pyramid 实例上拥有多个域和子域.但是,我似乎找不到任何关于它的文档.最后一个问题引用了一个词汇表,其中的信息很少,也没有示例.你们中有没有人有任何示例或可以指导我获得更好的文档?

I'm looking to have multiple domains and subdomains on a single Pyramid instance. However, I can't seem to find any documentation on it. The last question referred to a glossary with very little information and no examples. Do any of you have any examples or can direct me to better documentation?

推荐答案

Pyramid 只是一个 WSGI 应用程序.这意味着它依赖于 HTTP_HOST 环境键(由 Host 标头设置)来确定应用程序的主机.都是相对的.重点是 Pyramid 对它可以接受的内容没有任何限制,因此世界就是你的牡蛎,你可以将它设置为将内容限制在各种领域.这当然首先要从您的网络服务器配置为提供给您的应用程序的主机开始.

Pyramid is just a WSGI application. This means it's dependent on the HTTP_HOST environ key (set by the Host header) to determine the host of the application. It's all relative. Point-being that Pyramid has no restrictions on what it can accept, thus the world is your oyster and you can set it up to limit content to various domains however you'd like. This of course starts with what hosts your webserver is configured to feed to your application.

假设您正在使用 URL 分派,您可能想要设计一些自定义路由谓词来检查 request.host 值以获取您想要的任何值.从该谓词返回 False 将阻止该路由匹配对该主机的请求.

Assuming you're using URL dispatch, you might want to design some custom route predicates that check the request.host value for whatever you'd like. Returning False from that predicate will prevent that route from ever matching a request to that host.

这是一个很大的话题,因此如果您提供更多细节可能会有所帮助.例如,由于 Pyramid 是相对的,因此您可能希望从example.com"生成的任何 URL 以将某人重定向到sub.example.com",都需要通过预生成器完成.

This is a large topic, so it might help if you give some more specifics. For example, since Pyramid is relative, any URL you may want to generate from 'example.com' to redirect someone to 'sub.example.com' will need to be done via a pregenerator.

def pregen(request, elements, kw):
    kw['_app_url'] = 'http://sub.example.com'
    return elements, kw

def req_sub(info, request):
    return request.host.startswith('sub')

config.add_route('sub_only', '/',
                 custom_predicates=(req_sub,),
                 pregenerator=pregen)
config.add_route('foo', '/foo')
config.add_view(view, route_name-'foo')

def view(request):
    # redirect the user to "http://sub.example.com", regardless of whether
    # request.host is "example.com" or "sub.example.com"
    return HTTPFound(request.route_url('sub_only'))

这篇关于单个 Pyramid 实例上的多个域和子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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