sanic/examples/vhosts.py

41 lines
914 B
Python
Raw Normal View History

2017-05-13 15:01:35 +01:00
from sanic.response import text
2017-01-08 23:48:12 +00:00
from sanic import Sanic
2017-01-11 02:07:58 +00:00
from sanic.blueprints import Blueprint
2017-01-08 23:48:12 +00:00
# Usage
# curl -H "Host: example.com" localhost:8000
# curl -H "Host: sub.example.com" localhost:8000
2017-01-11 02:07:58 +00:00
# curl -H "Host: bp.example.com" localhost:8000/question
# curl -H "Host: bp.example.com" localhost:8000/answer
2017-01-08 23:48:12 +00:00
app = Sanic()
2017-01-11 02:07:58 +00:00
bp = Blueprint("bp", host="bp.example.com")
2017-01-08 23:48:12 +00:00
2017-05-13 15:01:35 +01:00
2017-01-19 03:40:20 +00:00
@app.route('/', host=["example.com",
"somethingelse.com",
"therestofyourdomains.com"])
2017-05-13 15:01:35 +01:00
async def hello1(request):
return text("Some defaults")
2017-01-19 03:40:20 +00:00
2017-01-08 23:48:12 +00:00
@app.route('/', host="sub.example.com")
2017-05-13 15:01:35 +01:00
async def hello2(request):
return text("42")
2017-01-08 23:48:12 +00:00
2017-01-11 02:07:58 +00:00
@bp.route("/question")
2017-05-13 15:01:35 +01:00
async def hello3(request):
return text("What is the meaning of life?")
2017-01-11 02:07:58 +00:00
@bp.route("/answer")
2017-05-13 15:01:35 +01:00
async def hello4(request):
return text("42")
2017-01-11 02:07:58 +00:00
app.blueprint(bp)
2017-01-11 02:07:58 +00:00
2017-01-08 23:48:12 +00:00
if __name__ == '__main__':
2017-05-13 15:01:35 +01:00
app.run(host="127.0.0.1", port=8000)