四個(gè)都是 Tornado 的模塊席怪,在本例中都是必須的哆窿。它們四個(gè)在一般的網(wǎng)站開(kāi)發(fā)中,都要用到,基本作用分別是:
tornado.httpserver:這個(gè)模塊就是用來(lái)解決 web 服務(wù)器的 http 協(xié)議問(wèn)題态贤,它提供了不少屬性方法鸣戴,實(shí)現(xiàn)客戶端和服務(wù)器端的互通损拢。Tornado 的非阻塞行冰、單線程的特點(diǎn)在這個(gè)模塊中體現(xiàn)。
tornado.ioloop:這個(gè)也非常重要避乏,能夠?qū)崿F(xiàn)非阻塞 socket 循環(huán)爷耀,不能互通一次就結(jié)束。
tornado.options:這是命令行解析模塊拍皮,也常用到歹叮。
tornado.web:這是必不可少的模塊,它提供了一個(gè)簡(jiǎn)單的 Web 框架與異步功能铆帽,從而使其擴(kuò)展到大量打開(kāi)的連接咆耿,使其成為理想的長(zhǎng)輪詢。
設(shè)置靜態(tài)路徑
app?=?tornado.web.Application(
handlers=[(r'/',?IndexHandler),?(r'/poem',?MungedPageHandler)],
template_path=os.path.join(os.path.dirname(__file__),"templates"),
static_path=os.path.join(os.path.dirname(__file__),"static"),
debug=True
)
連接mongo
frompymongoimportMongoClient
#?建立于MongoClient?的連接
client?=?MongoClient("localhost",27017)
#?得到數(shù)據(jù)庫(kù)爹橱,只有插入數(shù)據(jù)時(shí)才會(huì)創(chuàng)建:
db?=?client.cache
#?或者
#?db?=?client['cache']
print(db)
#?得到一個(gè)數(shù)據(jù)集合萨螺,只有插入數(shù)據(jù)時(shí)才會(huì)創(chuàng)建:
collection?=?db.test_collection
#?或者? ? collection?=?db['test-collection']
#?MongoDB中的數(shù)據(jù)使用的是類似Json風(fēng)格的文檔:
importdatetime
post={
"author":"Mike",
"test":"My?first?blog?post",
"tags":["mongodb","python","pymongo"],
"date":datetime.datetime.utcnow()
}
#?插入一個(gè)文檔
posts?=?db.posts
post_id?=?posts.insert_one(post).inserted_id
print(post_id)
代碼
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)
client = MongoClient('127.0.0.1', 27017)
db = client.homework
coll = db.homework1
class IndexHandler(tornado.web.RequestHandler):
? ? def get(self):
? ? ? ? greeting = self.get_argument('greeting', 'Hello')
? ? ? ? self.write(greeting + ', friendly user!')
#主方法
if __name__ == "__main__":
? ? #解析命令行參數(shù)
? ? tornado.options.parse_command_line()
? ? #創(chuàng)建應(yīng)用程序
? ? app = tornado.web.Application(
? ? ? ? handlers=[(r"/", IndexHandler)],
? ? ? ? template_path=os.path.join(os.path.dirname(__file__),? ? ? ? ? ? ? ? ? ? "templates/basic"),
? ? ? ? static_path=os.path.join(os.path.dirname(__file__),"static"),
? ? )
? ? #創(chuàng)建Http服務(wù)器
? ? http_server = tornado.httpserver.HTTPServer(app)
? ? #設(shè)置監(jiān)聽(tīng)端口
? ? http_server.listen(options.port)
? ? #啟動(dòng)服務(wù)器循環(huán)
? ? tornado.ioloop.IOLoop.instance().start()
包裝Application
class Application(tornado.web.Application):
? ? def __init__(self):
? ? ? ? handlers = [
? ? ? ? ? ? (r"/", MainHandler),
? ? ? ? ]
? ? ? ? settings = dict(
? ? ? ? ? ? template_path=os.path.join(os.path.dirname(__file__), "templates"),
? ? ? ? ? ? static_path=os.path.join(os.path.dirname(__file__), "static"),
? ? ? ? ? ? debug=True,
? ? ? ? )
? ? ? ? tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == "__main__":
? ? tornado.options.parse_command_line()
? ? http_server = tornado.httpserver.HTTPServer(Application())
? ? http_server.listen(options.port)
? ? tornado.ioloop.IOLoop.instance().start()