Dynamic Subdomain Handling in a Web App (Flask)

All Flask's routing constructs support the subdomain keyword argument (this includes support for route variables).

@app.route("/", subdomain="static")
def static_index():
    """Flask supports static subdomains
    This is available at static.your-domain.tld"""
    return "static.your-domain.tld"

@app.route("/dynamic", subdomain="<username>")
def username_index(username):
    """Dynamic subdomains are also supported
    Try going to user1.your-domain.tld/dynamic"""
    return username + ".your-domain.tld"

To complement Sean Viera's post, you also need to set the SERVER_NAME config variable.

Documentation: http://flask.pocoo.org/docs/config/#SERVER_NAME

The name and port number of the server. Required for subdomain support (e.g.: 'myapp.dev:5000') Note that localhost does not support subdomains so setting this to “localhost” does not help. Setting a SERVER_NAME also by default enables URL generation without a request context but with an application context.

To test locally you need to add entries to your hosts file, like this:

127.0.0.1       cvshark.local
127.0.0.1       robert.cvshark.local
127.0.0.1       www.cvshark.local