File: multidomainapp.py

package info (click to toggle)
gunicorn 20.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,088 kB
  • sloc: python: 9,219; makefile: 161; xml: 73; sh: 40; javascript: 35
file content (36 lines) | stat: -rw-r--r-- 993 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.

import re

class SubDomainApp:
    """WSGI application to delegate requests based on domain name.
"""
    def __init__(self, mapping):
        self.mapping = mapping

    def __call__(self, environ, start_response):
        host = environ.get("HTTP_HOST", "")
        host = host.split(":")[0]  # strip port

        for pattern, app in self.mapping:
            if re.match("^" + pattern + "$", host):
                return app(environ, start_response)
        else:
            start_response("404 Not Found", [])
            return [b""]

def hello(environ, start_response):
    start_response("200 OK", [("Content-Type", "text/plain")])
    return [b"Hello, world\n"]

def bye(environ, start_response):
    start_response("200 OK", [("Content-Type", "text/plain")])
    return [b"Goodbye!\n"]

app = SubDomainApp([
    ("localhost", hello),
    (".*", bye)
])