demo1: simple hello world
main.py
#!/usr/bin/env python
#-*-coding: utf-8-*-
# Version: 0.1
# Author: Song Huang <[email protected]>
# License: Copyright(c) 2015 Song.Huang
# Summary:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, type=int)
#handler 类中的方法都是get、post、delete等
class IndexHandler(tornado.web.RequestHandler):
def get(self):
#get_argument take function method
res = self.get_argument('res', 'hello')
#write write to the web page
self.write(res + 'world!')
if __name__ == '__main__':
#Parse a command line
tornado.options.parse_command_line()
#Path matching
app = tornado.web.Application(
handlers = [(r'/', IndexHandler)]
) #Create
a httpserver instance
http_server = tornado.httpserver.HTTPServer(app)
#Listening port
http_server.listen(options.port)
#Startup item
tornado.ioloop.IOLoop.instance().start()
-------------------------------------------------------------------------------------------------
url access:
http://10.58.100.90:8000/
The page displays: hello world
http://10.58.100.90:8000/?res
The page displays: world!
--------------------------------------------------------
demo2: Simple template application
main.py
#!/usr/bin/env python
#-*-coding: utf-8-*-
# Version: 0.1
# Author: Song Huang <[email protected]>
# License: Copyright(c) 2015 Song.Huang
# Summary:
import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
#渲染模板render
self.render('index.html')
class ContentHandler(tornado.web.RequestHandler):
def post(self):
n1 = self.get_argument('n1')
n2 = self.get_argument('n2')
n3 = self.get_argument('n3')
n4 = self.get_argument('n4')
self.render('content.html', n1=n1, n2=n2, n3=n3,n4=n4)
if __name__ == '__main__':
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[(r'/', IndexHandler), (r'/content', ContentHandler)],
#Template path
template_path=os.path.join(os.path.dirname(__file__), "templates"),
#静态文件路径 js、css
static_path = os.path.join(os.path.dirname(__file__), "static")
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
There is another file index.html content.html under templates folder
index.html
<!DOCTYPE html>
<html>
<head><title>Index</title></head>
<body>
//apply static resources
<href='static/1.css'>
//Fast loading of static resources
<href='{{static_url("1.css")}}'>
<form method="post" action="/content">
<p>N1<br><input type="text" name="n1"></p>
<p>N2<br><input type="text" name="n2"></p>
<p>N3<br><input type="text" name="n3"></p>
<p>N4<br><input type="text" name="n4"></p>
<input type="submit">
</form>
</body>
</html>
content.html
<!DOCTYPE html>
<html>
<head><title>Content</title></head>
<body>
<h1>n1: {{n1}}</h1>
<h1>n2: {{n2}}</h1>
<h1>n3: {{n3}}</h1>
<h1>n4: {{n4}}</h1>
{% if content %}
{%end%}
{%for i in content%}
{%end%}
</body>
</html>
----------------------------------------------------------------------------------
The method of page parameter {{}} is similar to django
----------------------------------------------------------------------------------
demo3: