sanic/examples/vhosts.py

39 lines
937 B
Python
Raw Normal View History

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