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

Subdomain in Flask | Python

Last Updated : 17 Apr, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
Python | Using for loop in Flask
Prerequisite: HTML Basics, Python Basics, Flask It is not possible to write front-end course every time user make changes in his/her profile. We use a template and it generates code according to the content. Flask is one of the web development frameworks written in Python. Through flask, a loop can be run in the HTML code using jinja template and a
3 min read
Python | Build a REST API using Flask
Prerequisite: Introduction to Rest API REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data. In this article, we will build a REST API in Python using the Flask framework. Flask is a popular micro framework f
3 min read
Create a Weather app using Flask | Python
Prerequisite : Flask installation Flask is a lightweight framework written in Python. It is lightweight because it does not require particular tools or libraries and allow rapid web development. today we will create a weather app using flask as a web framework. this weather web app will provide current weather updates of cities searched. Basic setu
2 min read
Todo list app using Flask | Python
There are many frameworks that allow building your webpage using Python, like Django, flask, etc. Flask is a web application framework written in Python. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine. Its modules and libraries that help the developer to write applications without writing the low-level codes
3 min read
Profile Application using Python Flask and MySQL
A framework is a code library that makes a developer's life easier when building web applications by providing reusable code for common operations. There are a number of frameworks for Python, including Flask, Tornado, Pyramid, and Django. Flask is a lightweight web application framework. It is classified as a micro-framework because it does not re
10 min read
Create a Real Time Currency Converter app using Flask | Python
Prerequisite: installation of PythonWhat is Flask in Python ? Flask is a popular and lightweight Python web framework, meaning it is a third-party Python library used for developing web applications. Project Setup : create a new directory and name it 'CURRENCY-CONVERTER'. mkdir CURRENCY-CONVERTER && cd CURRENCY-CONVERTER Inside it create tw
8 min read
Autocomplete input suggestion using Python and Flask
In this article, we will learn how to give features like auto-completion to the data that is passed from Flask. Autocomplete basically means predicting the rest of the word when the user types something. This way human interaction also increases with every correct prediction. Let's see how to do the same. We will be using jquery for autocompletion.
2 min read
Basic Validation using flask-gladiator module in Python
All kinds of validation are essential nowadays, considering all the unstructured data flowing in and out of systems. Apart from client-side validation, server-side validations are equally essential in web development. This article discusses a way in which validation can be injected in various frameworks such as Flask or Django. Python's flask-gladi
3 min read
Python Flask - Request Object
In a Flask App, we have our own Webpage (Client) and a Server. The Server should process the data. The Request, in Flask, is an object that contains all the data sent from the Client to Server. This data can be recovered using the GET/POST Methods. POST is used when your application expects user input to be received by command or an HTTP request, w
5 min read
Create GitHub API to fetch user profile image and number of repositories using Python and Flask
GitHub is where developers shape the future of software, together, contribute to the open-source community, manage Git repositories, etc. It is one of the most used tools by a developer and its profile is shared to showcase or let others contribute to its projects. Web Scraping using python is also one of the best methods to get data. In this artic
5 min read
Convert CSV to HTML Table using Python Pandas and Flask Framework
In this article, we are going to convert a CSV file into an HTML table using Python Pandas and Flask Framework. Sample CSV file : USERNAME,IDENTIFIER,FIRST_NAME,LAST_NAME booker12,9012,Rachel,Booker grey07,2070,Laura,Grey johnson81,4081,Craig,Johnson jenkins46,9346,Mary,Jenkins smith79,5079,Jamie,SmithStepwise ImplementationCreating Environment Ste
2 min read
How to Upload File in Python-Flask
File uploading is a typical task in web apps. Taking care of file upload in Flask is simple all we need is to have an HTML form with the encryption set to multipart/form information to publish the file into the URL. The server-side flask script brings the file from the request object utilizing the request.files[] Object. On effectively uploading th
2 min read
Python Flask - Redirect and Errors
We'll discuss redirects and errors with Python Flask in this article. A redirect is used in the Flask class to send the user to a particular URL with the status code. conversely, this status code additionally identifies the issue. When we access a website, our browser sends a request to the server, and the server replies with what is known as the H
4 min read
Responsive Chart with Bokeh, Flask and Python
In this post, we will use the Flask framework to create our web application in Python. The Bokeh library will be used to create interactive graphs and we will visualize this graph through a simple frontend HTML page. For this, we will first write the endpoints in Flask which will help us to create Bokeh charts, and then we will create HTML template
5 min read
How to Install Flask-CORS in Python
In this article, we will learn to install Flask-CORS which is a Python module and a Flask extension that supports and allows cross-origin resource sharing (CORS) through a simple decorator. Due to security concerns, cross-domain cookie submission is disabled by default. We will look at how to install Flask-Cors on the Linux operating system. What i
3 min read
How to Build a Web App using Flask and SQLite in Python
Python-based Flask is a microweb framework. Typically, a micro-framework has little to no dependencies on outside frameworks. Despite being a micro framework, practically everything may be developed when and as needed utilizing Python libraries and other dependencies. In this post, we'll develop a Flask application that collects user input in a for
4 min read
Show Data on Google Pie Chart using Python Flask
In this article, we are going to build a mini project by using a Flask using Python and that will demonstrate to us the use of a google pie chart. A single web page on which input will be taken from users e.g an activity for how long they are performing. After clicking on submit it will generate a pie chart. It can be integrated into our applicatio
5 min read
Flask login without database - Python
In this article, we will talk about Python-based Flask login without a database in this article. In order to use Flask login without a database in this method basically we are using a python dictionary through which users will be able to log in using their login credentials that we add to the database and also the functionality of logout will be ad
4 min read
Deploy Python Flask App on Heroku
Flask is a web application framework written in Python. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects. This article revolves around how to deploy a flask app on Heroku. To demonstrate this, we are first going to create a sample application for a better understanding of the process. Prerequisites Pyt
2 min read
How PyCharm supports Flask in Python?
Flask is a reliable framework for Python web applications and APIs. It is a well-liked option among developers due to its simplicity and adaptability. But to fully realize its potential, effective instruments are needed. PyCharm shows up as a powerful ally with a ton of capabilities designed specifically for Flask development. Let's explore what Py
4 min read
Article Tags :
Practice Tags :