I've been playing with Tornado recently, so I'll show you how to use it.
This time, it's almost an official Top rehash. Please look forward to it next time. If you have any questions or requests, we will answer them.
My execution environment is as follows, but I think that there is no problem if it is an environment where Python works.
Tornado is a web framework / asynchronous communication library written in Python and has the following features.
From now on, we will handle everything from installation to Hello, world.
Install Tornado with pip.
$ pip install tornado
If you have not installed pip, you should install it by referring to the following site.
If you don't want to use pip, drop tar.gz and use setup.py as usual.
tar xvzf tornado-3.2.2.tar.gz
cd tornado-3.2.2
python setup.py build
sudo python setup.py install
Hello, world Once installed, all you have to do is import, write the appropriate code, and listen. Here, as the simplest example, let's display the string "Hello, world" in the browser. To display Hello, world, save and execute the following code in an appropriate directory with an appropriate name (server.py in this case).
server.py
#!/bin/env python
# -*- coding: utf-8 -*-
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()
Just give python a script.
$ python server.py
Nothing is displayed in the terminal, but if you access it with a browser in this state, "Hello, world" will be displayed.
It's boring just to display the string, so at least I'll try to return the html file. Prepare the style sheet and HTML file, and rewrite server.py as follows. With this knowledge, you should be able to build an ancient website that returns properly styled static pages.
Prepare style.css in the static directory and index.html in the templates directory.
$ tree --charset=x
.
|-- server.py
|-- static
| `-- style.css
`-- templates
`-- index.html
The main changes in server.py are as follows.
server.py
#!/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
application = tornado.web.Application([
(r"/", MainHandler)
],
template_path=os.path.join(os.getcwd(), "templates"),
static_path=os.path.join(os.getcwd(), "static"),
)
if __name__ == "__main__":
application.listen(8888)
print("Server is up ...")
tornado.ioloop.IOLoop.instance().start()
style.css
body {
font-family:'Lucida Grande', 'Hiragino Kaku Gothic ProN', 'Hiragino Kakugo ProN W3', "MS Gothic", sans-serif;
width: 80%;
margin: 0 auto;
}
p {
color:#BEAF71;
font-size:200%;
}
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello, world</title>
<link rel="stylesheet" href="{{ static_url("style.css") }}"/>
</head>
<body>
<div id="container">
<div id="main">
<p>Hello, world</p>
</div>
</div>
</body>
</html>
$ python server.py
print("Server is up ...")
You can see that the HTML is certainly returned.
Recommended Posts