-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
vhosts.py
45 lines (29 loc) · 1011 Bytes
/
vhosts.py
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
37
38
39
40
41
42
43
44
45
from sanic import Sanic, response
from sanic.blueprints import Blueprint
# Usage
# curl -H "Host: example.com" localhost:8000
# curl -H "Host: sub.example.com" localhost:8000
# curl -H "Host: bp.example.com" localhost:8000/question
# curl -H "Host: bp.example.com" localhost:8000/answer
app = Sanic("Example")
bp = Blueprint("bp", host="bp.example.com")
@app.route(
"/", host=["example.com", "somethingelse.com", "therestofyourdomains.com"]
)
async def hello_0(request):
return response.text("Some defaults")
@app.route("/", host="sub.example.com")
async def hello_1(request):
return response.text("42")
@bp.route("/question")
async def hello_2(request):
return response.text("What is the meaning of life?")
@bp.route("/answer")
async def hello_3(request):
return response.text("42")
@app.get("/name")
def name(request):
return response.text(request.app.url_for("name", _external=True))
app.blueprint(bp)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)