有段時間沒來更新這個網誌了,最近在如火如荼的進行著專題抗戰,現在就來寫一篇最近學的framework。
系統環境:Ubuntu 14.04 Server、Python3.4、Tornado 4.0.2、MongoDB 2.6、PyMongo
Tornado全稱Tornado Web Server,是一個用Python語言寫成的Web伺服器兼Web應用框架,由FriendFeed公司在自己的網站FriendFeed中使用,被Facebook收購以後框架以開源軟體形式開放給大眾。 以上節錄自Wiki
簡介就自行去估一下吧!就不進行簡介了。
我的架構大概像這樣,有點在抄django的寫法。
/ static/ --> 靜態檔案。
/ templates/ --> 範本(HTML檔案)。
/ manage.py --> 執行的檔案。
/ view.py --> 主程式。
/ models.py --> 模型(資料庫操作)。
manage.py
# -*- coding: utf-8 -*-
#!/usr/bin/env python3.4
import os, sys, views
import tornado.httpserver, tornado.ioloop, tornado.web, tornado.autoreload
from tornado.options import define, options
define('port', default=8000, help='run on the given port', type=int)
def main():
tornado.options.parse_command_line()
app = views.App()
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
tornado.autoreload.start()
for dir, _, files in os.walk('templates'):
[tornado.autoreload.watch(dir + '/' + f) for f in files if not f.startswith('.')]
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()
#!/usr/bin/env python3.4
import os, sys, views
import tornado.httpserver, tornado.ioloop, tornado.web, tornado.autoreload
from tornado.options import define, options
define('port', default=8000, help='run on the given port', type=int)
def main():
tornado.options.parse_command_line()
app = views.App()
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
tornado.autoreload.start()
for dir, _, files in os.walk('templates'):
[tornado.autoreload.watch(dir + '/' + f) for f in files if not f.startswith('.')]
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()
view.py
# -*- coding: utf-8 -*-
#!/usr/bin/env python3.4
import models, os
import tornado.httpserver, tornado.ioloop, tornado.web
class App(tornado.web.Application):
# Application init settings
def __init__(self):
handlers = [
(r'/', MainHandler),
]
settings = {
'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
'cookie_secret':
'GKPu279PjY28Kkb2z3TA6N3ue497Z3haiQHzT4ti6JWrq5ds8H2GP6qT85ZR77MU',
'/login': True,
'xsrf_cookies': True,
'debug': True
}
self.db = models.dbConnection()
tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
#!/usr/bin/env python3.4
import models, os
import tornado.httpserver, tornado.ioloop, tornado.web
class App(tornado.web.Application):
# Application init settings
def __init__(self):
handlers = [
(r'/', MainHandler),
]
settings = {
'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
'cookie_secret':
'GKPu279PjY28Kkb2z3TA6N3ue497Z3haiQHzT4ti6JWrq5ds8H2GP6qT85ZR77MU',
'/login': True,
'xsrf_cookies': True,
'debug': True
}
self.db = models.dbConnection()
tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
models.py
# -*- coding: utf-8 -*-
#!/usr/bin/env python3.4
from pymongo import MongoClient
settings = {
'host': '資料庫位置',# Database's host.
'port': '資料庫的Port', # Database's port.
}
def dbConnection():
# 資料庫連線
db = MongoClient(**settings)['自行設定你的資料庫']
return db
#!/usr/bin/env python3.4
from pymongo import MongoClient
settings = {
'host': '資料庫位置',# Database's host.
'port': '資料庫的Port', # Database's port.
}
def dbConnection():
# 資料庫連線
db = MongoClient(**settings)['自行設定你的資料庫']
return db
index.html
跟上篇我自己的宣告