当前位置: 首页>>代码示例>>Python>>正文


Python Response.headers["location"]方法代码示例

本文整理汇总了Python中pylons.Response.headers["location"]方法的典型用法代码示例。如果您正苦于以下问题:Python Response.headers["location"]方法的具体用法?Python Response.headers["location"]怎么用?Python Response.headers["location"]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pylons.Response的用法示例。


在下文中一共展示了Response.headers["location"]方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __call__

# 需要导入模块: from pylons import Response [as 别名]
# 或者: from pylons.Response import headers["location"] [as 别名]
    def __call__(self, environ, start_response):
        g = config["pylons.g"]
        http_host = environ.get("HTTP_HOST", "localhost").lower()
        domain, s, port = http_host.partition(":")

        # remember the port
        try:
            environ["request_port"] = int(port)
        except ValueError:
            pass

        # localhost is exempt so paster run/shell will work
        # media_domain doesn't need special processing since it's just ads
        if domain == "localhost" or is_subdomain(domain, g.media_domain):
            return self.app(environ, start_response)

        # tell reddit_base to redirect to the appropriate subreddit for
        # a legacy CNAME
        if not is_subdomain(domain, g.domain):
            environ["legacy-cname"] = domain
            return self.app(environ, start_response)

        # figure out what subdomain we're on if any
        subdomains = domain[: -len(g.domain) - 1].split(".")
        extension_subdomains = dict(m="mobile", i="compact", api="api", rss="rss", xml="xml", json="json")

        sr_redirect = None
        for subdomain in subdomains[:]:
            if subdomain in g.reserved_subdomains:
                continue

            extension = extension_subdomains.get(subdomain)
            if extension:
                environ["reddit-domain-extension"] = extension
            elif self.lang_re.match(subdomain):
                environ["reddit-prefer-lang"] = subdomain
                environ["reddit-domain-prefix"] = subdomain
            else:
                sr_redirect = subdomain
                subdomains.remove(subdomain)

        # if there was a subreddit subdomain, redirect
        if sr_redirect and environ.get("FULLPATH"):
            r = Response()
            if not subdomains and g.domain_prefix:
                subdomains.append(g.domain_prefix)
            subdomains.append(g.domain)
            redir = "%s/r/%s/%s" % (".".join(subdomains), sr_redirect, environ["FULLPATH"])
            redir = "http://" + redir.replace("//", "/")
            r.status_code = 301
            r.headers["location"] = redir
            r.content = ""
            return r(environ, start_response)

        return self.app(environ, start_response)
开发者ID:jorik041,项目名称:reddit,代码行数:57,代码来源:middleware.py

示例2: __call__

# 需要导入模块: from pylons import Response [as 别名]
# 或者: from pylons.Response import headers["location"] [as 别名]
    def __call__(self, environ, start_response):
        # get base domain as defined in INI file
        base_domain = config["global_conf"]["domain"]
        try:
            sub_domains, request_port = environ["HTTP_HOST"].split(":")
            environ["request_port"] = int(request_port)
        except ValueError:
            sub_domains = environ["HTTP_HOST"].split(":")[0]
        except KeyError:
            sub_domains = "localhost"

        # If the domain doesn't end with base_domain, assume
        # this is a cname, and redirect to the frame controller.
        # Ignore localhost so paster shell still works.
        # If this is an error, don't redirect
        if not sub_domains.endswith(base_domain) and (not sub_domains == "localhost"):
            environ["sub_domain"] = sub_domains
            if not environ.get("extension"):
                if environ["PATH_INFO"].startswith("/frame"):
                    return self.app(environ, start_response)
                elif self.is_auth_cname(sub_domains):
                    environ["frameless_cname"] = True
                    environ["authorized_cname"] = True
                elif (
                    "redditSession=cname" in environ.get("HTTP_COOKIE", "")
                    and environ["REQUEST_METHOD"] != "POST"
                    and not environ["PATH_INFO"].startswith("/error")
                ):
                    environ["original_path"] = environ["PATH_INFO"]
                    environ["FULLPATH"] = environ["PATH_INFO"] = "/frame"
                else:
                    environ["frameless_cname"] = True
            return self.app(environ, start_response)

        sub_domains = sub_domains[: -len(base_domain)].strip(".")
        sub_domains = sub_domains.split(".")

        sr_redirect = None
        for sd in list(sub_domains):
            # subdomains to disregard completely
            if sd in ("www", "origin", "beta", "lab", "pay", "buttons", "ssl"):
                continue
            # subdomains which change the extension
            elif sd == "m":
                environ["reddit-domain-extension"] = "mobile"
            elif sd == "I":
                environ["reddit-domain-extension"] = "compact"
            elif sd == "i":
                environ["reddit-domain-extension"] = "compact"
            elif sd in ("api", "rss", "xml", "json"):
                environ["reddit-domain-extension"] = sd
            elif (len(sd) == 2 or (len(sd) == 5 and sd[2] == "-")) and self.lang_re.match(sd):
                environ["reddit-prefer-lang"] = sd
                environ["reddit-domain-prefix"] = sd
            else:
                sr_redirect = sd
                sub_domains.remove(sd)

        if sr_redirect and environ.get("FULLPATH"):
            r = Response()
            sub_domains.append(base_domain)
            redir = "%s/r/%s/%s" % (".".join(sub_domains), sr_redirect, environ["FULLPATH"])
            redir = "http://" + redir.replace("//", "/")
            r.status_code = 301
            r.headers["location"] = redir
            r.content = ""
            return r(environ, start_response)

        return self.app(environ, start_response)
开发者ID:Julian,项目名称:reddit,代码行数:71,代码来源:middleware.py


注:本文中的pylons.Response.headers["location"]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。