Tornado 是 FriendFeed 使用的可擴展的非阻塞式 web 服務器及其相關工具的開源版本。這個 Web 框架看起來有些像web.py 或者 Google 的 webapp,不過爲了能有效利用非阻塞式服務器環境,這個 Web 框架還包含了一些相關的有用工具 和優化。css
Tornado 和如今的主流 Web 服務器框架(包括大多數 Python 的框架)有着明顯的區別:它是非阻塞式服務器,並且速度至關快。得利於其 非阻塞的方式和對 epoll 的運用,Tornado 每秒能夠處理數以千計的鏈接,這意味着對於實時 Web 服務來講,Tornado 是一個理想的 Web 框架。咱們開發這個 Web 服務器的主要目的就是爲了處理 FriendFeed 的實時功能 ——在 FriendFeed 的應用裏每個活動用戶都會保持着一個服務器鏈接。(關於如何擴容 服務器,以處理數以千計的客戶端的鏈接的問題,請參閱 C10K problem。)html
1
2
3
|
pip install tornado
源碼安裝
https:
/
/
pypi.python.org
/
packages
/
source
/
t
/
tornado
/
tornado
-
4.3
.tar.gz
|
1、快速上手node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
tornado.ioloop
import
tornado.web
class
MainHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.write(
"Hello, world"
)
application
=
tornado.web.Application([
(r
"/index"
, MainHandler),
])
if
__name__
=
=
"__main__"
:
application.listen(
8888
)
tornado.ioloop.IOLoop.instance().start()
|
第一步:執行腳本,監聽 8888 端口python
第二步:瀏覽器客戶端訪問 /index --> http://127.0.0.1:8888/indexjquery
第三步:服務器接受請求,並交由對應的類處理該請求git
第四步:類接受到請求以後,根據請求方式(post / get / delete ...)的不一樣調用並執行相應的方法程序員
第五步:方法返回值的字符串內容發送瀏覽器github
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #!/usr/bin/env python 4 # -*- coding:utf-8 -*- 5 6 import tornado.ioloop 7 import tornado.web 8 from tornado import httpclient 9 from tornado.web import asynchronous 10 from tornado import gen 11 12 import uimodules as md 13 import uimethods as mt 14 15 class MainHandler(tornado.web.RequestHandler): 16 @asynchronous 17 @gen.coroutine 18 def get(self): 19 print 'start get ' 20 http = httpclient.AsyncHTTPClient() 21 http.fetch("http://127.0.0.1:8008/post/", self.callback) 22 self.write('end') 23 24 def callback(self, response): 25 print response.body 26 27 settings = { 28 'template_path': 'template', 29 'static_path': 'static', 30 'static_url_prefix': '/static/', 31 'ui_methods': mt, 32 'ui_modules': md, 33 } 34 35 application = tornado.web.Application([ 36 (r"/index", MainHandler), 37 ], **settings) 38 39 40 if __name__ == "__main__": 41 application.listen(8009) 42 tornado.ioloop.IOLoop.instance().start()
2、路由系統web
路由系統其實就是 url 和 類 的對應關係,這裏不一樣於其餘框架,其餘不少框架均是 url 對應 函數,Tornado中每一個url對應的是一個類。ajax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
tornado.ioloop
import
tornado.web
class
MainHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.write(
"Hello, world"
)
class
StoryHandler(tornado.web.RequestHandler):
def
get(
self
, story_id):
self
.write(
"You requested the story "
+
story_id)
class
BuyHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.write(
"buy.wupeiqi.com/index"
)
application
=
tornado.web.Application([
(r
"/index"
, MainHandler),
(r
"/story/([0-9]+)"
, StoryHandler),
])
application.add_handlers(
'buy.wupeiqi.com$'
, [
(r
'/index'
,BuyHandler),
])
if
__name__
=
=
"__main__"
:
application.listen(
80
)
tornado.ioloop.IOLoop.instance().start()
|
3、模板
Tornao中的模板語言和django中相似,模板引擎將模板文件載入內存,而後將數據嵌入其中,最終獲取到一個完整的字符串,再將字符串返回給請求者。
Tornado 的模板支持「控制語句」和「表達語句」,控制語句是使用 {%
和 %}
包起來的 例如 {% if len(items) > 2 %}
。表達語句是使用 {{
和 }}
包起來的,例如 {{ items[0] }}
。
控制語句和對應的 Python 語句的格式基本徹底相同。咱們支持 if
、for
、while
和 try
,這些語句邏輯結束的位置須要用 {% end %}
作標記。還經過 extends
和 block
語句實現了模板繼承。這些在 template
模塊 的代碼文檔中有着詳細的描述
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 5 <title>老男孩</title> 6 <link href="{{static_url("css/common.css")}}" rel="stylesheet" /> 7 {% block CSS %}{% end %} 8 </head> 9 <body> 10 11 <div class="pg-header"> 12 13 </div> 14 15 {% block RenderBody %}{% end %} 16 17 <script src="{{static_url("js/jquery-1.8.2.min.js")}}"></script> 18 19 {% block JavaScript %}{% end %} 20 </body> 21 </html>
1 {% extends 'layout.html'%} 2 {% block CSS %} 3 <link href="{{static_url("css/index.css")}}" rel="stylesheet" /> 4 {% end %} 5 6 {% block RenderBody %} 7 <h1>Index</h1> 8 9 <ul> 10 {% for item in li %} 11 <li>{{item}}</li> 12 {% end %} 13 </ul> 14 15 {% end %} 16 17 {% block JavaScript %} 18 19 {% end %}
1 import tornado.ioloop 2 import tornado.web 3 4 5 class MainHandler(tornado.web.RequestHandler): 6 def get(self): 7 self.render('home/index.html') 8 9 settings = { 10 'template_path': 'template', 11 } 12 13 application = tornado.web.Application([ 14 (r"/index", MainHandler), 15 ], **settings) 16 17 18 if __name__ == "__main__": 19 application.listen(80) 20 tornado.ioloop.IOLoop.instance().start()
在模板中默認提供了一些函數、字段、類以供模板使用:
escape
: tornado.escape.xhtml_escape
的別名xhtml_escape
: tornado.escape.xhtml_escape
的別名url_escape
: tornado.escape.url_escape
的別名json_encode
: tornado.escape.json_encode
的別名squeeze
: tornado.escape.squeeze
的別名linkify
: tornado.escape.linkify
的別名datetime
: Python 的 datetime
模組handler
: 當前的 RequestHandler
對象request
: handler.request
的別名current_user
: handler.current_user
的別名locale
: handler.locale
的別名_
: handler.locale.translate
的別名static_url
: for handler.static_url
的別名xsrf_form_html
: handler.xsrf_form_html
的別名Tornado默認提供的這些功能其實本質上就是 UIMethod 和 UIModule,咱們也能夠自定義從而實現相似於Django的simple_tag的功能:
一、定義
1 # uimethods.py 2 3 def tab(self): 4 return 'UIMethod'
1 #uimodules.py 2 3 from tornado.web import UIModule 4 from tornado import escape 5 6 class custom(UIModule): 7 8 def render(self, *args, **kwargs): 9 return escape.xhtml_escape('<h1>wupeiqi</h1>') 10 #return escape.xhtml_escape('<h1>wupeiqi</h1>')
二、註冊
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #!/usr/bin/env python 4 # -*- coding:utf-8 -*- 5 6 import tornado.ioloop 7 import tornado.web 8 from tornado.escape import linkify 9 import uimodules as md 10 import uimethods as mt 11 12 class MainHandler(tornado.web.RequestHandler): 13 def get(self): 14 self.render('index.html') 15 16 settings = { 17 'template_path': 'template', 18 'static_path': 'static', 19 'static_url_prefix': '/static/', 20 'ui_methods': mt, 21 'ui_modules': md, 22 } 23 24 application = tornado.web.Application([ 25 (r"/index", MainHandler), 26 ], **settings) 27 28 29 if __name__ == "__main__": 30 application.listen(8009) 31 tornado.ioloop.IOLoop.instance().start()
三、使用
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <link href="{{static_url("commons.css")}}" rel="stylesheet" /> 7 </head> 8 <body> 9 <h1>hello</h1> 10 {% module custom(123) %} 11 {{ tab() }} 12 </body>
4、實用功能
一、靜態文件
對於靜態文件,能夠配置靜態文件的目錄和前段使用時的前綴,而且Tornaodo還支持靜態文件緩存。
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import tornado.ioloop 5 import tornado.web 6 7 8 class MainHandler(tornado.web.RequestHandler): 9 def get(self): 10 self.render('home/index.html') 11 12 settings = { 13 'template_path': 'template', 14 'static_path': 'static', 15 'static_url_prefix': '/static/', 16 } 17 18 application = tornado.web.Application([ 19 (r"/index", MainHandler), 20 ], **settings) 21 22 23 if __name__ == "__main__": 24 application.listen(80) 25 tornado.ioloop.IOLoop.instance().start()
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <link href="{{static_url("commons.css")}}" rel="stylesheet" /> 7 </head> 8 <body> 9 <h1>hello</h1> 10 </body> 11 </html>
備註:靜態文件緩存的實現
1 def get_content_version(cls, abspath): 2 """Returns a version string for the resource at the given path. 3 4 This class method may be overridden by subclasses. The 5 default implementation is a hash of the file's contents. 6 7 .. versionadded:: 3.1 8 """ 9 data = cls.get_content(abspath) 10 hasher = hashlib.md5() 11 if isinstance(data, bytes): 12 hasher.update(data) 13 else: 14 for chunk in data: 15 hasher.update(chunk) 16 return hasher.hexdigest()
二、csrf
Tornado中的跨站請求僞造和Django中的類似,跨站僞造請求(Cross-site request forgery)
1 settings = { 2 "xsrf_cookies": True, 3 } 4 application = tornado.web.Application([ 5 (r"/", MainHandler), 6 (r"/login", LoginHandler), 7 ], **settings)
1 # 普通表單使用 2 3 <form action="/new_message" method="post"> 4 {{ xsrf_form_html() }} 5 <input type="text" name="message"/> 6 <input type="submit" value="Post"/> 7 </form>
1 # Ajax使用 2 3 function getCookie(name) { 4 var r = document.cookie.match("\\b" + name + "=([^;]*)\\b"); 5 return r ? r[1] : undefined; 6 } 7 8 jQuery.postJSON = function(url, args, callback) { 9 args._xsrf = getCookie("_xsrf"); 10 $.ajax({url: url, data: $.param(args), dataType: "text", type: "POST", 11 success: function(response) { 12 callback(eval("(" + response + ")")); 13 }}); 14 };
注:Ajax使用時,本質上就是去獲取本地的cookie,攜帶cookie再來發送請求
三、cookie
Tornado中能夠對cookie進行操做,而且還能夠對cookie進行簽名以放置僞造。
a、基本操做
1 class MainHandler(tornado.web.RequestHandler): 2 def get(self): 3 if not self.get_cookie("mycookie"): 4 self.set_cookie("mycookie", "myvalue") 5 self.write("Your cookie was not set yet!") 6 else: 7 self.write("Your cookie was set!") 8 9 Code
b、簽名
Cookie 很容易被惡意的客戶端僞造。加入你想在 cookie 中保存當前登錄用戶的 id 之類的信息,你須要對 cookie 做簽名以防止僞造。Tornado 經過 set_secure_cookie 和 get_secure_cookie 方法直接支持了這種功能。 要使用這些方法,你須要在建立應用時提供一個密鑰,名字爲 cookie_secret。 你能夠把它做爲一個關鍵詞參數傳入應用的設置中:
1 class MainHandler(tornado.web.RequestHandler): 2 def get(self): 3 if not self.get_secure_cookie("mycookie"): 4 self.set_secure_cookie("mycookie", "myvalue") 5 self.write("Your cookie was not set yet!") 6 else: 7 self.write("Your cookie was set!") 8 9 application = tornado.web.Application([ 10 (r"/", MainHandler), 11 ], cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=")
1 def _create_signature_v1(secret, *parts): 2 hash = hmac.new(utf8(secret), digestmod=hashlib.sha1) 3 for part in parts: 4 hash.update(utf8(part)) 5 return utf8(hash.hexdigest()) 6 7 8 def _create_signature_v2(secret, s): 9 hash = hmac.new(utf8(secret), digestmod=hashlib.sha256) 10 hash.update(utf8(s)) 11 return utf8(hash.hexdigest())
1 def create_signed_value(secret, name, value, version=None, clock=None, 2 key_version=None): 3 if version is None: 4 version = DEFAULT_SIGNED_VALUE_VERSION 5 if clock is None: 6 clock = time.time 7 8 timestamp = utf8(str(int(clock()))) 9 value = base64.b64encode(utf8(value)) 10 if version == 1: 11 signature = _create_signature_v1(secret, name, value, timestamp) 12 value = b"|".join([value, timestamp, signature]) 13 return value 14 elif version == 2: 15 # The v2 format consists of a version number and a series of 16 # length-prefixed fields "%d:%s", the last of which is a 17 # signature, all separated by pipes. All numbers are in 18 # decimal format with no leading zeros. The signature is an 19 # HMAC-SHA256 of the whole string up to that point, including 20 # the final pipe. 21 # 22 # The fields are: 23 # - format version (i.e. 2; no length prefix) 24 # - key version (integer, default is 0) 25 # - timestamp (integer seconds since epoch) 26 # - name (not encoded; assumed to be ~alphanumeric) 27 # - value (base64-encoded) 28 # - signature (hex-encoded; no length prefix) 29 def format_field(s): 30 return utf8("%d:" % len(s)) + utf8(s) 31 to_sign = b"|".join([ 32 b"2", 33 format_field(str(key_version or 0)), 34 format_field(timestamp), 35 format_field(name), 36 format_field(value), 37 b'']) 38 39 if isinstance(secret, dict): 40 assert key_version is not None, 'Key version must be set when sign key dict is used' 41 assert version >= 2, 'Version must be at least 2 for key version support' 42 secret = secret[key_version] 43 44 signature = _create_signature_v2(secret, to_sign) 45 return to_sign + signature 46 else: 47 raise ValueError("Unsupported version %d" % version)
1 def _decode_signed_value_v1(secret, name, value, max_age_days, clock): 2 parts = utf8(value).split(b"|") 3 if len(parts) != 3: 4 return None 5 signature = _create_signature_v1(secret, name, parts[0], parts[1]) 6 if not _time_independent_equals(parts[2], signature): 7 gen_log.warning("Invalid cookie signature %r", value) 8 return None 9 timestamp = int(parts[1]) 10 if timestamp < clock() - max_age_days * 86400: 11 gen_log.warning("Expired cookie %r", value) 12 return None 13 if timestamp > clock() + 31 * 86400: 14 # _cookie_signature does not hash a delimiter between the 15 # parts of the cookie, so an attacker could transfer trailing 16 # digits from the payload to the timestamp without altering the 17 # signature. For backwards compatibility, sanity-check timestamp 18 # here instead of modifying _cookie_signature. 19 gen_log.warning("Cookie timestamp in future; possible tampering %r", 20 value) 21 return None 22 if parts[1].startswith(b"0"): 23 gen_log.warning("Tampered cookie %r", value) 24 return None 25 try: 26 return base64.b64decode(parts[0]) 27 except Exception: 28 return None 29 30 31 def _decode_fields_v2(value): 32 def _consume_field(s): 33 length, _, rest = s.partition(b':') 34 n = int(length) 35 field_value = rest[:n] 36 # In python 3, indexing bytes returns small integers; we must 37 # use a slice to get a byte string as in python 2. 38 if rest[n:n + 1] != b'|': 39 raise ValueError("malformed v2 signed value field") 40 rest = rest[n + 1:] 41 return field_value, rest 42 43 rest = value[2:] # remove version number 44 key_version, rest = _consume_field(rest) 45 timestamp, rest = _consume_field(rest) 46 name_field, rest = _consume_field(rest) 47 value_field, passed_sig = _consume_field(rest) 48 return int(key_version), timestamp, name_field, value_field, passed_sig 49 50 51 def _decode_signed_value_v2(secret, name, value, max_age_days, clock): 52 try: 53 key_version, timestamp, name_field, value_field, passed_sig = _decode_fields_v2(value) 54 except ValueError: 55 return None 56 signed_string = value[:-len(passed_sig)] 57 58 if isinstance(secret, dict): 59 try: 60 secret = secret[key_version] 61 except KeyError: 62 return None 63 64 expected_sig = _create_signature_v2(secret, signed_string) 65 if not _time_independent_equals(passed_sig, expected_sig): 66 return None 67 if name_field != utf8(name): 68 return None 69 timestamp = int(timestamp) 70 if timestamp < clock() - max_age_days * 86400: 71 # The signature has expired. 72 return None 73 try: 74 return base64.b64decode(value_field) 75 except Exception: 76 return None 77 78 79 def get_signature_key_version(value): 80 value = utf8(value) 81 version = _get_version(value) 82 if version < 2: 83 return None 84 try: 85 key_version, _, _, _, _ = _decode_fields_v2(value) 86 except ValueError: 87 return None 88 89 return key_version
簽名Cookie的本質是:
寫cookie過程:
- 將值進行base64加密
- 對除值之外的內容進行簽名,哈希算法(沒法逆向解析)
- 拼接 簽名 + 加密值
讀cookie過程:
- 讀取 簽名 + 加密值
- 對簽名進行驗證
- base64解密,獲取值內容
注:許多API驗證機制和安全cookie的實現機制相同。
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import tornado.ioloop 5 import tornado.web 6 7 8 class MainHandler(tornado.web.RequestHandler): 9 10 def get(self): 11 login_user = self.get_secure_cookie("login_user", None) 12 if login_user: 13 self.write(login_user) 14 else: 15 self.redirect('/login') 16 17 18 class LoginHandler(tornado.web.RequestHandler): 19 def get(self): 20 self.current_user() 21 22 self.render('login.html', **{'status': ''}) 23 24 def post(self, *args, **kwargs): 25 26 username = self.get_argument('name') 27 password = self.get_argument('pwd') 28 if username == 'wupeiqi' and password == '123': 29 self.set_secure_cookie('login_user', '武沛齊') 30 self.redirect('/') 31 else: 32 self.render('login.html', **{'status': '用戶名或密碼錯誤'}) 33 34 settings = { 35 'template_path': 'template', 36 'static_path': 'static', 37 'static_url_prefix': '/static/', 38 'cookie_secret': 'aiuasdhflashjdfoiuashdfiuh' 39 } 40 41 application = tornado.web.Application([ 42 (r"/index", MainHandler), 43 (r"/login", LoginHandler), 44 ], **settings) 45 46 47 if __name__ == "__main__": 48 application.listen(8888) 49 tornado.ioloop.IOLoop.instance().start()
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import tornado.ioloop 5 import tornado.web 6 7 class BaseHandler(tornado.web.RequestHandler): 8 9 def get_current_user(self): 10 return self.get_secure_cookie("login_user") 11 12 class MainHandler(BaseHandler): 13 14 @tornado.web.authenticated 15 def get(self): 16 login_user = self.current_user 17 self.write(login_user) 18 19 20 21 class LoginHandler(tornado.web.RequestHandler): 22 def get(self): 23 self.current_user() 24 25 self.render('login.html', **{'status': ''}) 26 27 def post(self, *args, **kwargs): 28 29 username = self.get_argument('name') 30 password = self.get_argument('pwd') 31 if username == 'wupeiqi' and password == '123': 32 self.set_secure_cookie('login_user', '武沛齊') 33 self.redirect('/') 34 else: 35 self.render('login.html', **{'status': '用戶名或密碼錯誤'}) 36 37 settings = { 38 'template_path': 'template', 39 'static_path': 'static', 40 'static_url_prefix': '/static/', 41 'cookie_secret': 'aiuasdhflashjdfoiuashdfiuh', 42 'login_url': '/login' 43 } 44 45 application = tornado.web.Application([ 46 (r"/index", MainHandler), 47 (r"/login", LoginHandler), 48 ], **settings) 49 50 51 if __name__ == "__main__": 52 application.listen(8888) 53 tornado.ioloop.IOLoop.instance().start()
5、擴展功能
一、自定義Session
a.知識儲備
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class
Foo(
object
):
def
__getitem__(
self
, key):
print
'__getitem__'
,key
def
__setitem__(
self
, key, value):
print
'__setitem__'
,key,value
def
__delitem__(
self
, key):
print
'__delitem__'
,key
obj
=
Foo()
result
=
obj[
'k1'
]
#obj['k2'] = 'wupeiqi'
#del obj['k1']
|
b.session實現機制
1 import tornado.ioloop 2 import tornado.web 3 from hashlib import sha1 4 import os, time 5 6 session_container = {} 7 8 create_session_id = lambda: sha1('%s%s' % (os.urandom(16), time.time())).hexdigest() 9 10 11 class Session(object): 12 13 session_id = "__sessionId__" 14 15 def __init__(self, request): 16 session_value = request.get_cookie(Session.session_id) 17 if not session_value: 18 self._id = create_session_id() 19 else: 20 self._id = session_value 21 request.set_cookie(Session.session_id, self._id) 22 23 def __getitem__(self, key): 24 return session_container[self._id][key] 25 26 def __setitem__(self, key, value): 27 if session_container.has_key(self._id): 28 session_container[self._id][key] = value 29 else: 30 session_container[self._id] = {key: value} 31 32 def __delitem__(self, key): 33 del session_container[self._id][key] 34 35 36 class BaseHandler(tornado.web.RequestHandler): 37 38 def initialize(self): 39 # my_session['k1']訪問 __getitem__ 方法 40 self.my_session = Session(self) 41 42 43 class MainHandler(BaseHandler): 44 45 def get(self): 46 print self.my_session['c_user'] 47 print self.my_session['c_card'] 48 self.write('index') 49 50 class LoginHandler(BaseHandler): 51 52 def get(self): 53 self.render('login.html', **{'status': ''}) 54 55 def post(self, *args, **kwargs): 56 57 username = self.get_argument('name') 58 password = self.get_argument('pwd') 59 if username == 'wupeiqi' and password == '123': 60 61 self.my_session['c_user'] = 'wupeiqi' 62 self.my_session['c_card'] = '12312312309823012' 63 64 self.redirect('/index') 65 else: 66 self.render('login.html', **{'status': '用戶名或密碼錯誤'}) 67 68 settings = { 69 'template_path': 'template', 70 'static_path': 'static', 71 'static_url_prefix': '/static/', 72 'cookie_secret': 'aiuasdhflashjdfoiuashdfiuh', 73 'login_url': '/login' 74 } 75 76 application = tornado.web.Application([ 77 (r"/index", MainHandler), 78 (r"/login", LoginHandler), 79 ], **settings) 80 81 82 if __name__ == "__main__": 83 application.listen(8888) 84 tornado.ioloop.IOLoop.instance().start()
c. Session框架
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 import sys 5 import math 6 from bisect import bisect 7 8 9 if sys.version_info >= (2, 5): 10 import hashlib 11 md5_constructor = hashlib.md5 12 else: 13 import md5 14 md5_constructor = md5.new 15 16 17 class HashRing(object): 18 """一致性哈希""" 19 20 def __init__(self,nodes): 21 '''初始化 22 nodes : 初始化的節點,其中包含節點已經節點對應的權重 23 默認每個節點有32個虛擬節點 24 對於權重,經過多建立虛擬節點來實現 25 如:nodes = [ 26 {'host':'127.0.0.1:8000','weight':1}, 27 {'host':'127.0.0.1:8001','weight':2}, 28 {'host':'127.0.0.1:8002','weight':1}, 29 ] 30 ''' 31 32 self.ring = dict() 33 self._sorted_keys = [] 34 35 self.total_weight = 0 36 37 self.__generate_circle(nodes) 38 39 40 41 def __generate_circle(self,nodes): 42 for node_info in nodes: 43 self.total_weight += node_info.get('weight',1) 44 45 for node_info in nodes: 46 weight = node_info.get('weight',1) 47 node = node_info.get('host',None) 48 49 virtual_node_count = math.floor((32*len(nodes)*weight) / self.total_weight) 50 for i in xrange(0,int(virtual_node_count)): 51 key = self.gen_key_thirty_two( '%s-%s' % (node, i) ) 52 if self._sorted_keys.__contains__(key): 53 raise Exception('該節點已經存在.') 54 self.ring[key] = node 55 self._sorted_keys.append(key) 56 57 def add_node(self,node): 58 ''' 新建節點 59 node : 要添加的節點,格式爲:{'host':'127.0.0.1:8002','weight':1},其中第一個元素表示節點,第二個元素表示該節點的權重。 60 ''' 61 node = node.get('host',None) 62 if not node: 63 raise Exception('節點的地址不能爲空.') 64 65 weight = node.get('weight',1) 66 67 self.total_weight += weight 68 nodes_count = len(self._sorted_keys) + 1 69 70 virtual_node_count = math.floor((32 * nodes_count * weight) / self.total_weight) 71 for i in xrange(0,int(virtual_node_count)): 72 key = self.gen_key_thirty_two( '%s-%s' % (node, i) ) 73 if self._sorted_keys.__contains__(key): 74 raise Exception('該節點已經存在.') 75 self.ring[key] = node 76 self._sorted_keys.append(key) 77 78 def remove_node(self,node): 79 ''' 移除節點 80 node : 要移除的節點 '127.0.0.1:8000' 81 ''' 82 for key,value in self.ring.items(): 83 if value == node: 84 del self.ring[key] 85 self._sorted_keys.remove(key) 86 87 def get_node(self,string_key): 88 '''獲取 string_key 所在的節點''' 89 pos = self.get_node_pos(string_key) 90 if pos is None: 91 return None 92 return self.ring[ self._sorted_keys[pos]].split(':') 93 94 def get_node_pos(self,string_key): 95 '''獲取 string_key 所在的節點的索引''' 96 if not self.ring: 97 return None 98 99 key = self.gen_key_thirty_two(string_key) 100 nodes = self._sorted_keys 101 pos = bisect(nodes, key) 102 return pos 103 104 def gen_key_thirty_two(self, key): 105 106 m = md5_constructor() 107 m.update(key) 108 return long(m.hexdigest(), 16) 109 110 def gen_key_sixteen(self,key): 111 112 b_key = self.__hash_digest(key) 113 return self.__hash_val(b_key, lambda x: x) 114 115 def __hash_val(self, b_key, entry_fn): 116 return (( b_key[entry_fn(3)] << 24)|(b_key[entry_fn(2)] << 16)|(b_key[entry_fn(1)] << 8)| b_key[entry_fn(0)] ) 117 118 def __hash_digest(self, key): 119 m = md5_constructor() 120 m.update(key) 121 return map(ord, m.digest()) 122 123 124 """ 125 nodes = [ 126 {'host':'127.0.0.1:8000','weight':1}, 127 {'host':'127.0.0.1:8001','weight':2}, 128 {'host':'127.0.0.1:8002','weight':1}, 129 ] 130 131 ring = HashRing(nodes) 132 result = ring.get_node('98708798709870987098709879087') 133 print result 134 135 """
1 from hashlib import sha1 2 import os, time 3 4 5 create_session_id = lambda: sha1('%s%s' % (os.urandom(16), time.time())).hexdigest() 6 7 8 class Session(object): 9 10 session_id = "__sessionId__" 11 12 def __init__(self, request): 13 session_value = request.get_cookie(Session.session_id) 14 if not session_value: 15 self._id = create_session_id() 16 else: 17 self._id = session_value 18 request.set_cookie(Session.session_id, self._id) 19 20 def __getitem__(self, key): 21 # 根據 self._id ,在一致性哈西中找到其對應的服務器IP 22 # 找到相對應的redis服務器,如: r = redis.StrictRedis(host='localhost', port=6379, db=0) 23 # 使用python redis api 連接 24 # 獲取數據,即: 25 # return self._redis.hget(self._id, name) 26 27 def __setitem__(self, key, value): 28 # 根據 self._id ,在一致性哈西中找到其對應的服務器IP 29 # 使用python redis api 連接 30 # 設置session 31 # self._redis.hset(self._id, name, value) 32 33 34 def __delitem__(self, key): 35 # 根據 self._id 找到相對應的redis服務器 36 # 使用python redis api 連接 37 # 刪除,即: 38 return self._redis.hdel(self._id, name) 39
二、自定義模型版定
模型綁定有兩個主要功能:
在以前學習的Django中爲程序員提供了很是便捷的模型綁定功能,可是在Tornado中,一切須要本身動手!!!
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <link href="{{static_url("commons.css")}}" rel="stylesheet" /> 7 </head> 8 <body> 9 <h1>hello</h1> 10 <form action="/index" method="post"> 11 12 <p>hostname: <input type="text" name="host" /> </p> 13 <p>ip: <input type="text" name="ip" /> </p> 14 <p>port: <input type="text" name="port" /> </p> 15 <p>phone: <input type="text" name="phone" /> </p> 16 <input type="submit" /> 17 </form> 18 </body> 19 </html>
1 import tornado.ioloop 2 import tornado.web 3 from hashlib import sha1 4 import os, time 5 import re 6 7 8 class MainForm(object): 9 def __init__(self): 10 self.host = "(.*)" 11 self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" 12 self.port = '(\d+)' 13 self.phone = '^1[3|4|5|8][0-9]\d{8}$' 14 15 def check_valid(self, request): 16 form_dict = self.__dict__ 17 for key, regular in form_dict.items(): 18 post_value = request.get_argument(key) 19 # 讓提交的數據 和 定義的正則表達式進行匹配 20 ret = re.match(regular, post_value) 21 print key,ret,post_value 22 23 24 class MainHandler(tornado.web.RequestHandler): 25 def get(self): 26 self.render('index.html') 27 def post(self, *args, **kwargs): 28 obj = MainForm() 29 result = obj.check_valid(self) 30 self.write('ok') 31 32 33 34 settings = { 35 'template_path': 'template', 36 'static_path': 'static', 37 'static_url_prefix': '/static/', 38 'cookie_secret': 'aiuasdhflashjdfoiuashdfiuh', 39 'login_url': '/login' 40 } 41 42 application = tornado.web.Application([ 43 (r"/index", MainHandler), 44 ], **settings) 45 46 47 if __name__ == "__main__": 48 application.listen(8888) 49 tornado.ioloop.IOLoop.instance().start()
因爲請求的驗證時,須要考慮是否能夠爲空以及正則表達式的複用,因此:
1 # Form驗證框架 2 3 import tornado.ioloop 4 import tornado.web 5 import re 6 7 8 class Field(object): 9 10 def __init__(self, error_msg_dict, required): 11 self.id_valid = False 12 self.value = None 13 self.error = None 14 self.name = None 15 self.error_msg = error_msg_dict 16 self.required = required 17 18 def match(self, name, value): 19 self.name = name 20 21 if not self.required: 22 self.id_valid = True 23 self.value = value 24 else: 25 if not value: 26 if self.error_msg.get('required', None): 27 self.error = self.error_msg['required'] 28 else: 29 self.error = "%s is required" % name 30 else: 31 ret = re.match(self.REGULAR, value) 32 if ret: 33 self.id_valid = True 34 self.value = ret.group() 35 else: 36 if self.error_msg.get('valid', None): 37 self.error = self.error_msg['valid'] 38 else: 39 self.error = "%s is invalid" % name 40 41 42 class IPField(Field): 43 REGULAR = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" 44 45 def __init__(self, error_msg_dict=None, required=True): 46 47 error_msg = {} # {'required': 'IP不能爲空', 'valid': 'IP格式錯誤'} 48 if error_msg_dict: 49 error_msg.update(error_msg_dict) 50 51 super(IPField, self).__init__(error_msg_dict=error_msg, required=required) 52 53 54 class IntegerField(Field): 55 REGULAR = "^\d+$" 56 57 def __init__(self, error_msg_dict=None, required=True): 58 error_msg = {'required': '數字不能爲空', 'valid': '數字格式錯誤'} 59 if error_msg_dict: 60 error_msg.update(error_msg_dict) 61 62 super(IntegerField, self).__init__(error_msg_dict=error_msg, required=required) 63 64 65 class CheckBoxField(Field): 66 67 def __init__(self, error_msg_dict=None, required=True): 68 error_msg = {} # {'required': 'IP不能爲空', 'valid': 'IP格式錯誤'} 69 if error_msg_dict: 70 error_msg.update(error_msg_dict) 71 72 super(CheckBoxField, self).__init__(error_msg_dict=error_msg, required=required) 73 74 def match(self, name, value): 75 self.name = name 76 77 if not self.required: 78 self.id_valid = True 79 self.value = value 80 else: 81 if not value: 82 if self.error_msg.get('required', None): 83 self.error = self.error_msg['required'] 84 else: 85 self.error = "%s is required" % name 86 else: 87 if isinstance(name, list): 88 self.id_valid = True 89 self.value = value 90 else: 91 if self.error_msg.get('valid', None): 92 self.error = self.error_msg['valid'] 93 else: 94 self.error = "%s is invalid" % name 95 96 97 class FileField(Field): 98 REGULAR = "^(\w+\.pdf)|(\w+\.mp3)|(\w+\.py)$" 99 100 def __init__(self, error_msg_dict=None, required=True): 101 error_msg = {} # {'required': '數字不能爲空', 'valid': '數字格式錯誤'} 102 if error_msg_dict: 103 error_msg.update(error_msg_dict) 104 105 super(FileField, self).__init__(error_msg_dict=error_msg, required=required) 106 107 def match(self, name, value): 108 self.name = name 109 self.value = [] 110 if not self.required: 111 self.id_valid = True 112 self.value = value 113 else: 114 if not value: 115 if self.error_msg.get('required', None): 116 self.error = self.error_msg['required'] 117 else: 118 self.error = "%s is required" % name 119 else: 120 m = re.compile(self.REGULAR) 121 if isinstance(value, list): 122 for file_name in value: 123 r = m.match(file_name) 124 if r: 125 self.value.append(r.group()) 126 self.id_valid = True 127 else: 128 self.id_valid = False 129 if self.error_msg.get('valid', None): 130 self.error = self.error_msg['valid'] 131 else: 132 self.error = "%s is invalid" % name 133 break 134 else: 135 if self.error_msg.get('valid', None): 136 self.error = self.error_msg['valid'] 137 else: 138 self.error = "%s is invalid" % name 139 140 def save(self, request, upload_path=""): 141 142 file_metas = request.files[self.name] 143 for meta in file_metas: 144 file_name = meta['filename'] 145 with open(file_name,'wb') as up: 146 up.write(meta['body']) 147 148 149 class Form(object): 150 151 def __init__(self): 152 self.value_dict = {} 153 self.error_dict = {} 154 self.valid_status = True 155 156 def validate(self, request, depth=10, pre_key=""): 157 158 self.initialize() 159 self.__valid(self, request, depth, pre_key) 160 161 def initialize(self): 162 pass 163 164 def __valid(self, form_obj, request, depth, pre_key): 165 """ 166 驗證用戶表單請求的數據 167 :param form_obj: Form對象(Form派生類的對象) 168 :param request: Http請求上下文(用於從請求中獲取用戶提交的值) 169 :param depth: 對Form內容的深度的支持 170 :param pre_key: Html中name屬性值的前綴(多層Form時,內部遞歸時設置,無需理會) 171 :return: 是否驗證經過,True:驗證成功;False:驗證失敗 172 """ 173 174 depth -= 1 175 if depth < 0: 176 return None 177 form_field_dict = form_obj.__dict__ 178 for key, field_obj in form_field_dict.items(): 179 print key,field_obj 180 if isinstance(field_obj, Form) or isinstance(field_obj, Field): 181 if isinstance(field_obj, Form): 182 # 獲取以key開頭的全部的值,以參數的形式傳至 183 self.__valid(field_obj, request, depth, key) 184 continue 185 if pre_key: 186 key = "%s.%s" % (pre_key, key) 187 188 if isinstance(field_obj, CheckBoxField): 189 post_value = request.get_arguments(key, None) 190 elif isinstance(field_obj, FileField): 191 post_value = [] 192 file_list = request.request.files.get(key, None) 193 for file_item in file_list: 194 post_value.append(file_item['filename']) 195 else: 196 post_value = request.get_argument(key, None) 197 198 print post_value 199 # 讓提交的數據 和 定義的正則表達式進行匹配 200 field_obj.match(key, post_value) 201 if field_obj.id_valid: 202 self.value_dict[key] = field_obj.value 203 else: 204 self.error_dict[key] = field_obj.error 205 self.valid_status = False 206 207 208 class ListForm(object): 209 def __init__(self, form_type): 210 self.form_type = form_type 211 self.valid_status = True 212 self.value_dict = {} 213 self.error_dict = {} 214 215 def validate(self, request): 216 name_list = request.request.arguments.keys() + request.request.files.keys() 217 index = 0 218 flag = False 219 while True: 220 pre_key = "[%d]" % index 221 for name in name_list: 222 if name.startswith(pre_key): 223 flag = True 224 break 225 if flag: 226 form_obj = self.form_type() 227 form_obj.validate(request, depth=10, pre_key="[%d]" % index) 228 if form_obj.valid_status: 229 self.value_dict[index] = form_obj.value_dict 230 else: 231 self.error_dict[index] = form_obj.error_dict 232 self.valid_status = False 233 else: 234 break 235 236 index += 1 237 flag = False 238 239 240 class MainForm(Form): 241 242 def __init__(self): 243 # self.ip = IPField(required=True) 244 # self.port = IntegerField(required=True) 245 # self.new_ip = IPField(required=True) 246 # self.second = SecondForm() 247 self.fff = FileField(required=True) 248 super(MainForm, self).__init__() 249 250 # 251 # class SecondForm(Form): 252 # 253 # def __init__(self): 254 # self.ip = IPField(required=True) 255 # self.new_ip = IPField(required=True) 256 # 257 # super(SecondForm, self).__init__() 258 259 260 class MainHandler(tornado.web.RequestHandler): 261 def get(self): 262 self.render('index.html') 263 def post(self, *args, **kwargs): 264 # for i in dir(self.request): 265 # print i 266 # print self.request.arguments 267 # print self.request.files 268 # print self.request.query 269 # name_list = self.request.arguments.keys() + self.request.files.keys() 270 # print name_list 271 272 # list_form = ListForm(MainForm) 273 # list_form.validate(self) 274 # 275 # print list_form.valid_status 276 # print list_form.value_dict 277 # print list_form.error_dict 278 279 # obj = MainForm() 280 # obj.validate(self) 281 # 282 # print "驗證結果:", obj.valid_status 283 # print "符合驗證結果:", obj.value_dict 284 # print "錯誤信息:" 285 # for key, item in obj.error_dict.items(): 286 # print key,item 287 # print self.get_arguments('favor'),type(self.get_arguments('favor')) 288 # print self.get_argument('favor'),type(self.get_argument('favor')) 289 # print type(self.get_argument('fff')),self.get_argument('fff') 290 # print self.request.files 291 # obj = MainForm() 292 # obj.validate(self) 293 # print obj.valid_status 294 # print obj.value_dict 295 # print obj.error_dict 296 # print self.request,type(self.request) 297 # obj.fff.save(self.request) 298 # from tornado.httputil import HTTPServerRequest 299 # name_list = self.request.arguments.keys() + self.request.files.keys() 300 # print name_list 301 # print self.request.files,type(self.request.files) 302 # print len(self.request.files.get('fff')) 303 304 # obj = MainForm() 305 # obj.validate(self) 306 # print obj.valid_status 307 # print obj.value_dict 308 # print obj.error_dict 309 # obj.fff.save(self.request) 310 self.write('ok') 311 312 313 314 settings = { 315 'template_path': 'template', 316 'static_path': 'static', 317 'static_url_prefix': '/static/', 318 'cookie_secret': 'aiuasdhflashjdfoiuashdfiuh', 319 'login_url': '/login' 320 } 321 322 application = tornado.web.Application([ 323 (r"/index", MainHandler), 324 ], **settings) 325 326 327 if __name__ == "__main__": 328 application.listen(8888) 329 tornado.ioloop.IOLoop.instance().start()