Movatterモバイル変換


[0]ホーム

URL:


Open In App
Prerequisite:Introduction to FlaskIn 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 andwrite.geeksforgeeks.org are subdomains of thegeeksforgeeks.org domain, which in turn is a subdomain of theorg top-level domain (TLD).These are different from the path defined after TLD as ingeeksforgeeks.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 setuphosts 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/hostsWindows: C:\Windows\System32\Drivers\etc\hosts
Add these lines to set up alternate domain names.
127.0.0.1       vibhu.gfg127.0.0.1       practice.vibhu.gfg
In this example, we're consideringvibhu.gfg as our domain name, withgfg 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 configurationSERVER_NAME is set to the domain name, along with the port number we intend to run our app on. The default port, flask uses is5000, so we take it as it is.Python3 1==
fromflaskimportFlaskapp=Flask(__name__)@app.route('/')defhome():return"Welcome to GeeksForGeeks !"if__name__=="__main__":website_url='vibhu.gfg:5000'app.config['SERVER_NAME']=website_urlapp.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 thepractice subdomain.
  3. courses: An endpoint with extension on to the path on the practice subdomain.
Subdomains in Flask are set using thesubdomain parameter in theapp.route decorator.Python3 1==
fromflaskimportFlaskapp=Flask(__name__)@app.route('/')defhome():return"Welcome to GeeksForGeeks !"@app.route('/basic/')defbasic():return"Basic Category Articles " \"listed on this page."@app.route('/',subdomain='practice')defpractice():return"Coding Practice Page"@app.route('/courses/',subdomain='practice')defcourses():return"Courses listed " \"under practice subdomain."if__name__=="__main__":website_url='vibhu.gfg:5000'app.config['SERVER_NAME']=website_urlapp.run()
Output:

Improve
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp