首页 技术 正文
技术 2022年11月7日
0 收藏 370 点赞 599 浏览 2092 个字

作者:the5fire | 标签: MVC  tornado  | 发布:2012-08-06 2:41 p.m.

文接上篇,看我一个简单的helloworld,虽然觉得这个框架着实精小,但是实际开发总不能这么用。所以还是应该按照实际开发来写一个helloworld。

既然是实际项目版的helloworld,那就要有组织结构,不能代码都塞在一个文件里。

大体结构如下:

mvc_helloworld
--__init__.py
--urls.py
--application.py
--server.py
--handlers
----__init__.py
----index.py
--model
----__init__.py
----entity.py
--static
----css
------index.css
----js
----img
--templates
----index.html

这是一个简单的mvc结构,通过urls.py来控制访问,通过handlers来处理所有的访问,通过model来处理持久化的内容。剩下的static和templates就不用说了。另外可以通过在model和handlers之间增加cache层来提升性能。

下面逐一给出实例代码: server.py,用来启动web服务器:

#coding:utf-8import tornado.ioloop
import sysfrom application import applicationPORT = '8080'if __name__ == "__main__":
if len(sys.argv) > 1:
PORT = sys.argv[1]
application.listen(PORT)
print 'Development server is running at http://127.0.0.1:%s/' % PORT
print 'Quit the server with CONTROL-C'
tornado.ioloop.IOLoop.instance().start()

application.py,可以作为settings:

#coding:utf-8
#author:the5firefrom urls import urlsimport tornado.web
import os
SETTINGS = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
)application = tornado.web.Application(
handlers = urls,
**SETTINGS
)

urls.py:

#coding:utf-8from handlers.index import MainHandlerurls = [
(r'/', MainHandler),
]

handlers/index.py:

#coding:utf-8import tornado.web
from model.entity import Entityclass MainHandler(tornado.web.RequestHandler):
def get(self):
entity = Entity.get('the5fire\'s blog')
self.render('index.html', entity = entity)

model/entity.py:

#coding:utf-8class Entity(object):
def __init__(self, name):
self.name = name@staticmethod
def get(name):
return Entity(name)

templates/index.html:

<!DOCYTYPE html>
<html>
<head>
<meta type="utf-8">
<title>首页</title>
<link href="/static/css/index.css" rel="external nofollow" media="screen" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>Hello, tornado World!</h1>
<h2>by <a href="http://www.the5fire.com" rel="external nofollow" target="_blank">{{entity.name}}</a></h2>
</body>
</html>

static/css/index.css:

/**
author:the5fire
**/body {
background-color:#ccc;
}

大体上就这些,当然所有的东西都不是不可变的,应该按照自己的喜好来写。 最后运行的时候通过:python server.py 8000 代码可以在线查看,我的github库,有很多代码哦:https://github.com/the5fire/practice_demo/tree/master/learn_tornado/mvc_hello

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920