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!