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!