How to bind domain name and virtual host in Tornado server

Source: Internet
Author: User
Tornado default is to listen to IP plus port form, because tornado in the domestic use of very few people, data is the scale Mao Feng angle. Here's how tornado binds the domain name.

The default tornado Hello Word is here.
Copy the Code code 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, use the browser to access the ip+8888 port

If you want to support domain access, you can use the Nginx Monitor 80 port agent, or you have only such an application, directly with the Tornado monitoring 80 port without Nginx This step, the key is to do so others use IP or domain name can be accessed. Google has a lot of English site also can not find the binding domain name tutorial, anyway Tornado source file also on that point to look at, inside how to write? And then I saw this passage.

Exultation! Do not know why, this paragraph in the document is not, and then look down, return to the default is the host mechanism, and any host access,

In this case, we can use the domain name binding to run the Tornado application, and support multi-domain name, because that is a string of regular, on the basis of the example just added a domain binding

Copy the Code code 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 Results

You can even support regular matches, such as allowing it to also support www prefixes
Copy the Code code as follows:


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


Execution results

Combined with this mechanism, you can bind multiple domain names and sub-domain name multi-station deployments in one application. Easier to manage!

  • Related Article

    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.