Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subdomains with Flask?

Tags:

python

flask

web

How might I go about implementing subdomains for my website with Flask?

The documentation, though very good, isn't very clear at all about this. The subdomains need not to be dynamic, I am only going to be using 2 or 3 of my own choosing.

How would I route them? Is it possible to test them in the normal way? (served locally by Flask)

like image 903
user3233758 Avatar asked Jan 27 '14 18:01

user3233758


People also ask

Why do we need the domain name System DNS?

DNS ensures the internet is not only user-friendly but also works smoothly, loading whatever content we ask for quickly and efficiently. It's one of the cornerstones of how the internet operates. Without it, we'd be stuck memorizing long lists of numbers (IP addresses) to access the content we want.

How do you add a domain in python?

Just click the "Add a new web app" button on the Web tab. In the first step, it will ask you for the domain name to use. You should specify the fully-qualified domain name -- that is, www.yourdomain.com , not just yourdomain.com .

How do I change my flask hostname?

It is simple: 1) Go to this website : https://www.whatismyip.com/ ->find your IP address. After the above step restart your server, and try to hit your Flask Server remotely, it should work .


1 Answers

If all you want to do is handle having particular endpoints under a particular subdomain, you can just use the subdomain argument to @route:

app = Flask(__name__)
# In Flask 1.0
# app = Flask(__name__, subdomain_matching=True)

# Must add this until Flask 1.0
# Must be host:port pair or will not work
app.config["SERVER_NAME"] = "local.dev:5000"

@app.route("/")
def home():
    return "Sweet home"

@app.route("/some-route")
def some_route():
    return "on the default subdomain (generally, www, or unguarded)"

@app.route("/", subdomain="blog")
def blog_home():
    return "Sweet blog"

@app.route("/<page>", subdomain="blog")
def blog_page(page):
    return "can be dynamic: {}".format(page)

To handle development locally, you need to create entries in your hosts file to point these various domains to your machine:

local.dev    127.0.0.1
blog.local.dev    127.0.0.1

Then you can use local.dev and blog.local.dev rather than localhost to see your work.

like image 192
Sean Vieira Avatar answered Sep 27 '22 21:09

Sean Vieira