The Wayback Machine - https://web.archive.org/web/20240720124411/https://www.geeksforgeeks.org/subdomain-in-flask-python/
Open In App

Subdomain in Flask | Python

Last Updated : 17 Apr, 2019
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Prerequisite: Introduction to Flask

In this article, we will learn how to setup subdomains in Flask. But first, let’s go through the basic like what is DNS and subdomains.

Domain Name System (DNS):
The Domain Name System (DNS) is a hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or a private network. Most prominently, it translates more readily memorized domain names to the numerical IP addresses needed for locating and identifying computer services and devices with the underlying network protocols.
DNS is basically using words (Domain Names) in place of numbers (IP addresses) to locate something. For example, 127.0.0.1 is used to point the local computer address, localhost.

Subdomain:
A subdomain is a domain that is part of a larger domain. Basically, it’s a sort of child domain which means it is a part of some parent domain. For example, practice.geeksforgeeks.org and write.geeksforgeeks.org are subdomains of the geeksforgeeks.org domain, which in turn is a subdomain of the org top-level domain (TLD).
These are different from the path defined after TLD as in geeksforgeeks.org/basic/.

Further, we will discuss how to set endpoints in your web application using Python’s micro-framework, Flask.

Adding alternate domain name for local IP –
Prior to the coding part, we got to setup hosts file in order to provide alternate names to local IP so that we are able to test our app locally. Edit this file with root privileges.

Linux: /etc/hosts 
Windows: C:\Windows\System32\Drivers\etc\hosts

Add these lines to set up alternate domain names.

127.0.0.1       vibhu.gfg
127.0.0.1       practice.vibhu.gfg

In this example, we’re considering vibhu.gfg as our domain name, with gfg being the TLD. practice would be a subdomain we’re targeting to set in our web app.

Setting up the Server –
In the app’s configuration SERVER_NAME is set to the domain name, along with the port number we intend to run our app on. The default port, flask uses is 5000, so we take it as it is.




from flask import Flask
  
app = Flask(__name__)
  
  
@app.route('/')
def home():
    return "Welcome to GeeksForGeeks !"
  
  
if __name__ == "__main__":
    website_url = 'vibhu.gfg:5000'
    app.config['SERVER_NAME'] = website_url
    app.run()


Output:
Run the app and notice the link on which the app is running.

Test the link on your browser.

Adding Several Endpoints –

  1. basic: An endpoint with extension to the path on the main domain.
  2. practice: An endpoint serving on the practice subdomain.
  3. courses: An endpoint with extension on to the path on the practice subdomain.

Subdomains in Flask are set using the subdomain parameter in the app.route decorator.




from flask import Flask
  
app = Flask(__name__)
  
  
@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()


Output:



Previous Article
Next Article

Similar Reads

How to use Flask-Session in Python Flask ?
Flask Session - Flask-Session is an extension for Flask that supports Server-side Session to your application.The Session is the time between the client logs in to the server and logs out of the server.The data that is required to be saved in the Session is stored in a temporary directory on the server.The data in the Session is stored on the top o
4 min read
Documenting Flask Endpoint using Flask-Autodoc
Documentation of endpoints is an essential task in web development and being able to apply it in different frameworks is always a utility. This article discusses how endpoints in Flask can be decorated to generate good documentation of them using the Flask-Autodoc module. This module provides the following features - Helps to document endpoints of
4 min read
How to Integrate Flask-Admin and Flask-Login
In order to merge the admin and login pages, we can utilize a short form or any other login method that only requires the username and password. This is known as integrating the admin page and Flask login since it simply redirects users to the admin page when they log in. Let's is how to implement this in this article. Integrate Flask Admin and Fla
8 min read
Minify HTML in Flask using Flask-Minify
Flask offers HTML rendering as output, it is usually desired that the output HTML should be concise and it serves the purpose as well. In this article, we would display minification of output routes of Flask responses using the library - Flask-Minify. Advantages of MinificationWebsites load faster as fewer lines are there to upload and download.Ban
12 min read
Flask URL Helper Function - Flask url_for()
In this article, we are going to learn about the flask url_for() function of the flask URL helper in Python. Flask is a straightforward, speedy, scalable library, used for building, compact web applications. It is a micro framework, that presents developers, useful tools, and, features, for coding REST APIs, and backend data processing, of web apps
11 min read
How to Make a Subdomain Scanner in Python?
In this article, we are going to scan the subdomains using requests module in Python, which allows us to easily make HTTPS requests to get information from the websites. To install the requests module, write the following command in your command prompt. pip install requests The URL (Uniform Resource Locator) consists mainly of four parts: ProtocolS
6 min read
Massc - Subdomain Scanner Tool Designed in JavaScript
Subdomain enumeration is the process of finding valid (resolvable) subdomains for one or more domain(s). The general system is to use a dictionary of common names, trying to resolve them. These detected subdomains can reveal crucial information about the target, which might not be present in the main domain. Massc tool is a Nodejs language-based to
3 min read
Subdomain takeover from scratch to advance
Sub-domain Takeover : Sub-domain takeover is a common and most popular vulnerability. If you are not aware of such kind of vulnerability, you can understand it as a class of security issues where the intention of an attacker is to take control of an organization's sub-domain via cloud services. Sub-domain takeover vulnerability sometimes may lead t
2 min read
How to Setup a Subdomain on Your WordPress Website ?
Setting up a subdomain on your WordPress website can help you organize your content better and provide a dedicated space for specific sections of your site, such as a blog, shop, or forum. In this guide. we'll walk you through the steps to create and configure a subdomain for your WordPress website, ensuring the process is easy to follow and optimi
4 min read
Python Flask - ImmutableMultiDict
MultiDict is a sub-class of Dictionary that can contain multiple values for the same key, unlike normal Dictionaries. It is used because some form elements have multiple values for the same key and it saves the multiple values of a key in form of a list. Example: C/C++ Code from werkzeug.datastructures import MultiDict orders = MultiDict([(1, 'GFG'
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg