How to bind a domain name or virtual host to a Tornado server

Source: Internet
Author: User
This article mainly introduces how to bind domain names and virtual hosts to Tornado servers. I have checked the methods that Tornado has obtained, for more information, see Tornado. by default, Tornado listens to IP addresses and ports. Tornado is rarely used in China. The following describes how Tornado binds a domain name.

By default, the hello word of Tornado is like this.

The code is as follows:


Import tornado. ioloop
Import tornado. web

Class MainHandler (tornado. web. RequestHandler ):
Def get (self ):
Self. write ("Hello, world ")

Application = tornado. web. Application ([
(R "/", MainHandler ),
])

If _ name _ = "_ main __":
Application. listen (8888)
Tornado. ioloop. IOLoop. instance (). start ()

After running the command, use a browser to access the IP address + Port 8888

If you want to support domain name access, you can use Nginx to listen to port 80 as a proxy, or if you only have such an application, simply use Tornado to listen to port 80 to avoid Nginx, the key is that others can access the website using IP addresses or domain names. Google has a lot of English sites and cannot find the tutorial for binding domain names. anyway, the Tornado source file will be opened at that point. how can I write it inside? The result shows this passage.

Joy! I don't know why. This section does not exist in the document. then I will take a closer look at it. By default, the host mechanism is available for returning and any host access,

In this case, we can use the domain name binding method to run the tornado application, and support multiple domain names, because it is a string of regular expressions, add a domain name binding based on the example above.

The code is as follows:


Import tornado. ioloop
Import tornado. web

Class MainHandler (tornado. web. RequestHandler ):
Def get (self ):
Self. write ("Hello, world ")

Class DomainHandler (tornado. web. RequestHandler ):
Def get (self ):
Self. write ("Hello, a.com ")


Application = tornado. web. Application ([
(R "/", MainHandler ),
])

Application. add_handlers (r "^ a \. com $ ",[
(R "/", DomainHandler ),
])

If _ name _ = "_ main __":
Application. listen (8888)
Tornado. ioloop. IOLoop. instance (). start ()


A.com access result

It can even support Regular Expression Matching. for example, it can also support www prefixes.

The code is as follows:


Application. add_handlers (r "^ (www \.)? A \. com $ ", [(r"/", DomainHandler),])


Execution result

With this mechanism, you can bind multiple domain names and sub-domain names to a single application for multi-site deployment. Easy to manage!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.