librelist archives

« back to archive

Testing subdomains locally with flask's add_url_rule

Testing subdomains locally with flask's add_url_rule

From:
Raj Nathani
Date:
2013-02-03 @ 09:01
Using the conventional app.route method, the following code detects and 
prints the subdomain names
from flask import Flaskapp = Flask(__name__)
app.route("/", subdomain='<sub>')def hello_world(sub):    return "Hello 
World" % sub
app.config['SERVER_NAME'] = 'example.com'app.run(port=80)
On using add_url_rule, the following code is added instead of the app.route block
class Index(MethodView):    def get(self):        return "Hello World"
app.add_url_rule('/', view_func=Index.as_view('index'))
The first method works well, however the second one returns 404 whenever a
subdomainis entered, eg: 'www.example.com' / 'abc.example.com' however it 
will work for 'example.com'Note that I've added 'example.com', 
'www.example.com' and 'abc.example.com' in /etc/hosts, so thatsnot the 
problem, it even works well with the first method. However for some reason
flask rejects subdomainson the second method.Does anyone know of a fix?
Thanks,Relf

 		 	   		  

Re: [flask] Testing subdomains locally with flask's add_url_rule

From:
Ron DuPlain
Date:
2013-02-04 @ 18:49
On Sun, Feb 3, 2013 at 4:01 AM, Raj Nathani wrote:
> Does anyone know of a fix?

Without looking too deeply at your code listings, have you set
SERVER_NAME in the second approach? SERVER_NAME is needed for
subdomain routing because otherwise Flask will not know how to split
the hostname part of the URL.

http://flask.pocoo.org/docs/config/#builtin-configuration-values


> Note that I've added 'example.com', 'www.example.com' and 'abc.example.com'
> in /etc/hosts

An alternative to /etc/hosts is to set an A record and all of its
subdomains (wildcard) in DNS to 127.0.0.1. Something like:

    local.example.com A 127.0.0.1
    *.local.example.com A 127.0.0.1

I've seen a few publicly listed domain names for this, but cannot
speak to their reliability over time. Of course, this technique
requires a DNS call, so if you are working offline, you'll need to
fallback to /etc/hosts.

-Ron

Re: [flask] Testing subdomains locally with flask's add_url_rule

From:
Raj Nathani
Date:
2013-02-04 @ 21:02
The second piece of code replaces the hello_world function in the first 
piece, which are lines 4-6, it does include the server name.

I'm not too sure about what you mean by change the A records, If i'm not 
wrong I would have to own the domain to change its records, the testing is
local and offline with a reserved domain example.com

I don't think there is a work around while only using flask, I'm going to 
try putting flask behind apache and see if that works.

I'll keep you'll updated.

Thanks,
Raj

On 2013-02-04, at 1:59 PM, "Ron DuPlain" <ron.duplain@gmail.com> wrote:

> On Sun, Feb 3, 2013 at 4:01 AM, Raj Nathani wrote:
>> Does anyone know of a fix?
> 
> Without looking too deeply at your code listings, have you set
> SERVER_NAME in the second approach? SERVER_NAME is needed for
> subdomain routing because otherwise Flask will not know how to split
> the hostname part of the URL.
> 
> http://flask.pocoo.org/docs/config/#builtin-configuration-values
> 
> 
>> Note that I've added 'example.com', 'www.example.com' and 'abc.example.com'
>> in /etc/hosts
> 
> An alternative to /etc/hosts is to set an A record and all of its
> subdomains (wildcard) in DNS to 127.0.0.1. Something like:
> 
>    local.example.com A 127.0.0.1
>    *.local.example.com A 127.0.0.1
> 
> I've seen a few publicly listed domain names for this, but cannot
> speak to their reliability over time. Of course, this technique
> requires a DNS call, so if you are working offline, you'll need to
> fallback to /etc/hosts.
> 
> -Ron

Re: [flask] Testing subdomains locally with flask's add_url_rule

From:
Ron DuPlain
Date:
2013-02-04 @ 21:13
In your original code, you have:

    app.add_url_rule('/', view_func=Index.as_view('index'))

You are missing the subdomain argument. Note that app.add_url_rule
takes the same arguments as app.route. You need:

    app.add_url_rule('/', view_func=Index.as_view('index'), subdomain='<sub>')

More below.


On Mon, Feb 4, 2013 at 4:02 PM, Raj Nathani wrote:
> I'm not too sure about what you mean by change the A records,
> If i'm not wrong I would have to own the domain to change its records,
> the testing is local and offline with a reserved domain example.com

Maybe this is clearer:
http://jorgebernal.info/2009/07/17/42foo-virtual-hosts-web-development/

For Flask, you'd set SERVER_NAME to '42foo.com'. Then visit URLs:

    http://42foo.com/
    http://subdomain.42foo.com/

To view your application with subdomain routing. I do this with
local.example.com for my projects (which would have example.com as the
domain), as I don't want to depend on someone else's domain
registration.