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!