Subdomain in a flask | python

In this article, we will learn how to set up a subdomain in Flask. But first, let`s go over the basics like DNS and subdomains.

Domain Name System (DNS):
Domain Name System (DNS) — it is a hierarchical and decentralized naming system for computers, services, or other resources that are connected to the Internet or a private network. Most notably, it translates more easily remembered domain names into the numeric IP addresses needed to locate and identify computer services and devices with underlying network protocols. 
DNS basically uses words (domain names) instead of numbers (IP addresses) to find something. For example, is used to specify the address of the local computer localhost .

Subdomain — it is a domain that is part of a larger domain. Basically, it is a kind of child domain, which means that it is part of some parent domain. For example, practice.python.engineering and contribute.python.engineering are subdomains of the python.engineering domain, which in turn is a subdomain of org top level domain (TLD). 
They differ from the path defined after the TLD as in python.engineering/basic/ .

Next, we will discuss how to set endpoints in your web application with using the micro-framework Python, Flask.

Adding an alternate domain name for the local IP —
Before starting to write the code, we needed to configure the file hosts to provide local IP aliases so that we can test our application locally. Edit this file as superuser.

  Linux:  / etc / hosts  Windows:  C: WindowsSystem32Driversetchosts 

Add these lines, to set up alternate domain names. vibhu.gfg practice.vibhu.gfg 

In this example, we consider vibhu.gfg as our domain name, and gfg — TLD.  practice will be the subdomain that we target for installation in our web application.

Server setup —
In the application configuration SERVER_NAME set the domain name as well as the port number on which we are going to run our application. The default port that the flask uses is —  5000 , so we accept it as is.

from flask import Flask


app = Flask (__ name__)



@app . route ( ` / ` )

def home ():

return " Welcome to GeeksForGeeks! "



< p> if __ name__ = = "__ main__" :

website_url = `vibhu.gfg: 5000`

app.config [ `SERVER_NAME` ] = website_url

app.run ()

Run the application and pay attention to the link where it was launched. 

Check the link in your browser. 

Adding multiple endpoints —

  1. basic: an endpoint extending to a path in the primary domain.
  2. practice: an endpoint serving the subdomain practice .
  3. courses: an endpoint with a path extension to the practice subdomain.

Subdomains in Flask are specified using the subdomain in the app.route decorator.

from flask import Flask


app = Flask (__ name__)

 < / p>


@ app . route ( `/` )

def home ():

return "Welcome to GeeksForGeeks!"



@ app . route ( `/ basic /` )

def basic ():

return " Basic Category Articles "

  " listed on this page. "



@ app . route ( `/` , subdomain = `practice` )

def practice ():

return "Coding Practice Page"



@ app . route ( ` / courses / ` , subdomain = ` practice` )

def courses ():

return " Courses listed "

  " under practice subdomain. "



if __ name__ = = "__ main__" :

website_url = `vibhu.gfg: 5000`

  app.config [ `SERVER_NAME` ] = website_url

app.run ()



Get Solution for free from DataCamp guru