Tornado the method of binding domain name and virtual host in server _python

Source: Internet
Author: User

Tornado default is to monitor the IP plus port form, because tornado in the domestic use of very few people, the data is the scale Mao Feng angle. Let's say tornado how to bind a domain name.

The default tornado Hello Word is here.

Copy 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 name access, you can use Nginx to monitor 80 ports as agents, or you have only such an application, directly with tornado listening to 80 ports removed Nginx This step, the key is so do others use IP or domain name can access. Google a lot of English sites can not find the binding domain name tutorial, anyway Tornado source file also on that point to turn to see, inside how to write? It turns out that this is a phrase

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

In this way, we can use the domain binding to run the Tornado application, and support the multiple domain name, because that is a string of regular, on the basis of just that example, add a domain binding

Copy 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 a regular match, for example, to support the WWW prefix

Copy Code code as follows:

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

Execution results

Combined with this mechanism, you can bind multiple-station deployments of domain names and domain names across a single application. Easier 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.