圖片轉在自:http://www.javashuo.com/article/p-pwibfvqu-d.htmlhtml
圖片轉在自:http://www.javashuo.com/article/p-pwibfvqu-d.htmlpython
圖片轉在自:http://www.javashuo.com/article/p-pwibfvqu-d.htmlweb
接下來學習tornado數據庫
http://shouce.jb51.net/tornado/ch1.html#ch1-1-1 //學習網址瀏覽器
我是直接用得pycharm裝上就好 app
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):
greeting = self.get_argument('greeting', 'Hello')
self.write(greeting + ', friendly user!')
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()curl
halloword的的代碼 函數
代碼解讀://這裏來自教程tornado
編寫一個Tornado應用中最多的工做是定義類繼承Tornado的RequestHandler類。在這個例子中,咱們建立了一個簡單的應用,在給定的端口監聽請求,並在根目錄("/")響應請求。oop
你能夠在命令行裏嘗試運行這個程序以測試輸出:
$ python hello.py --port=8000
如今你能夠在瀏覽器中打開http://localhost:8000,或者打開另外一個終端窗口使用curl測試咱們的應用:
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8000/?greeting=Salutations
Salutations, friendly user!
讓咱們把這個例子分紅小塊,逐步分析它們:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
在程序的最頂部,咱們導入了一些Tornado模塊。雖然Tornado還有另一些有用的模塊,但在這個例子中咱們必須至少包含這四個模塊。
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
Tornado包括了一個有用的模塊(tornado.options)來從命令行中讀取設置。咱們在這裏使用這個模塊指定咱們的應用監聽HTTP請求的端口。它的工做流程以下:若是一個與define語句中同名的設置在命令行中被給出,那麼它將成爲全局options的一個屬性。若是用戶運行程序時使用了--help
選項,程序將打印出全部你定義的選項以及你在define函數的help參數中指定的文本。若是用戶沒有爲這個選項指定值,則使用default的值進行代替。Tornado使用type參數進行基本的參數類型驗證,當不合適的類型被給出時拋出一個異常。所以,咱們容許一個整數的port參數做爲options.port來訪問程序。若是用戶沒有指定值,則默認爲8000。
class IndexHandler(tornado.web.RequestHandler):
def get(self):
greeting = self.get_argument('greeting', 'Hello')
self.write(greeting + ', friendly user!')
這是Tornado的請求處理函數類。當處理一個請求時,Tornado將這個類實例化,並調用與HTTP請求方法所對應的方法。在這個例子中,咱們只定義了一個get方法,也就是說這個處理函數將對HTTP的GET請求做出響應。咱們稍後將看到實現不止一個HTTP方法的處理函數。
greeting = self.get_argument('greeting', 'Hello')
Tornado的RequestHandler類有一系列有用的內建方法,包括get_argument,咱們在這裏從一個查詢字符串中取得參數greeting的值。(若是這個參數沒有出如今查詢字符串中,Tornado將使用get_argument的第二個參數做爲默認值。)
self.write(greeting + ', friendly user!')
RequestHandler的另外一個有用的方法是write,它以一個字符串做爲函數的參數,並將其寫入到HTTP響應中。在這裏,咱們使用請求中greeting參數提供的值插入到greeting中,並寫回到響應中。
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
這是真正使得Tornado運轉起來的語句。首先,咱們使用Tornado的options模塊來解析命令行。而後咱們建立了一個Tornado的Application類的實例。傳遞給Application類__init__方法的最重要的參數是handlers。它告訴Tornado應該用哪一個類來響應請求。立刻咱們講解更多相關知識。
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
從這裏開始的代碼將會被反覆使用:一旦Application對象被建立,咱們能夠將其傳遞給Tornado的HTTPServer對象,而後使用咱們在命令行指定的端口進行監聽(經過options對象取出。)最後,在程序準備好接收HTTP請求後,咱們建立一個Tornado的IOLoop的實例。
而後這裏發現 運行事後 狀態碼會在運行框返回,這是在監聽並返回結果
上面說的哪一個教程網站有詳細的教程,這裏我只記下我本身以爲重要的東西,
###表單和模板
在html頁面中若是要填充代碼或者是參數,在html代碼中添加{{}}符號 ,教程中2.1例子很重要。
在{{}}裏面不只能夠添加變量還能夠添加表達式
{% %}中間添加控制語句 if for while try
Eg://這裏取自上面哪一個網頁教程
你一樣能夠在Tornado模板中使用Python條件和循環語句。控制語句以{%和%}包圍,並以相似下面的形式被使用:
{% if page is None %}
或
{% if len(entries) == 3 %}
控制語句的大部分就像對應的Python語句同樣工做,支持if、for、while和try。在這些狀況下,語句塊以{%開始,並以%}結束。
因此這個模板:
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>{{ header }}</h1>
<ul>
{% for book in books %}
<li>{{ book }}</li>
{% end %}
</ul>
</body>
</html>
當被下面這個處理函數調用時:
class BookHandler(tornado.web.RequestHandler):
def get(self):
self.render(
"book.html",
title="Home Page",
header="Books that are great",
books=[
"Learning Python",
"Programming Collective Intelligence",
"Restful Web Services"
]
)
將會渲染獲得下面的輸出:
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Books that are great</h1>
<ul>
<li>Learning Python</li>
<li>Programming Collective Intelligence</li>
<li>Restful Web Services</li>
</ul>
</body>
</html>
###數據庫 教程裏用的是pymongo