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,
127.0.0.1 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,
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
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.
127.0.0.1 vibhu.gfg 127.0.0.1 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.
Run the application and pay attention to the link where it was launched.
Check the link in your browser.
Adding multiple endpoints — p >
Subdomains in Flask are specified using the
subdomain in the
< / p>