Weitere ähnliche Inhalte Ähnlich wie 一种多屏时代的通用 web 应用架构 (20) Kürzlich hochgeladen (20) 一种多屏时代的通用 web 应用架构2. 自我介绍
●
game → webgame → web
●
珠三角技术沙龙组委、发起人之一
●
PyCon China 2011/2012 讲师
– Python 于 webgame 的应用
– 页游开发中的 Python 组件与模式
●
http://laiyonghao.com/
8. Web 架构的变迁( 2 )
●
def request_wants_json():
●
best = request.accept_mimetypes
●
.best_match(['application/json', 'text/html'])
●
return best == 'application/json' and
●
request.accept_mimetypes[best] >
●
request.accept_mimetypes['text/html']
9. Web 架构的变迁( 2 )
●
@app.route('/')
●
def show_items():
●
items = get_items_from_database()
●
if request_wants_json():
●
return jsonify(items=[x.to_json() for x in items])
●
return render_template('show_items.html',
items=items)
11. 插播: mimerender
●
mimerender = mimerender.FlaskMimeRender()
●
render_xml = lambda message: '<message>
%s</message>'%message
●
render_json = lambda **args: json.dumps(args)
●
render_html = lambda message: '<html><body>
%s</body></html>'%message
●
render_txt = lambda message: message
14. Web 架构的变迁( 2 )
browser
server
text/html
wap browser
app
text/vnd.wap.wml
application/json
16. Web 架构的变迁( 3 )
browser
adapter
text/html
wap browser
app
text/vnd.wap.wml
application/json
adapter
adapter
server
adapterSMS
application/json
17. Web 架构的变迁( 3 )
browser
adapter
text/html
wap browser
app
text/vnd.wap.wml
application/json
adapter
adapter
server
adapterSMS
application/json
?
18. Web 架构的变迁( 3 )
●
业务与展现分享
– server 专注业务实现,只需要提供 http api ,无须理
会数据展现形式;
– adapter 的业务简单,只需要维护会话,并对请求 / 响
应进行转换;
●
易于分工
– 内部可以分小组
– 甚至外包(解决了我团队不熟悉短信中心协议的问题)
●
易于扩展
– 支持更多设备(如自助终端、语音电话)
19. Web 架构的变迁( 3 )
●
安全:对数据进行签名
– client-id 、 client-key
– sha1
– 让 Date header 必填来确保每次请救不同
21. Flask-RESTful: Argument Parsing
●
parser = reqparse.RequestParser()
●
parser.add_argument('rate', type=int, help='Rate to
charge for this resource')
●
args = parser.parse_args()
●
支持必填项、多值项、重命名
●
可从 post body 、 query
string 、 headers 、 cookies 、 file uploads 中读取
22. Flask-RESTful: Output Fields
●
resource_fields = {
●
'name': fields.String,
●
'address': fields.String,
●
'date_updated': fields.DateTime,
●
}
●
class Todo(Resource):
●
@marshal_with(resource_fields)
●
def get(self, **kwargs):
●
return db_get_todo() # Some function that queries
the db
26. chtest
●
$ chtest --help
●
usage: chtest [-h] [--config-file CONFIG_FILE] --path
PATH [--method METHOD]
●
[--arg ARG] [--header HEADER] [--ensure-status
ENSURE_STATUS]
●
[--ensure-header ENSURE_HEADER]
●
[--ensure-content ENSURE_CONTENT] [--print-
header PRINT_HEADER]
●
[--print-json-path PRINT_JSON_PATH]
27. requests
●
chtest is requests powered
●
Requests: HTTP for Humans
– >>> r = requests.get('https://api.github.com/user',
auth=('user', 'pass'))
– >>> r.status_code
– 200
– >>> r.headers['content-type']
– 'application/json; charset=utf8'
– >>> r.text
– u'{"type":"User"...'
– >>> r.json()
29. Web 架构的变迁( 3 )
browser
adapter
text/html
wap browser
app
text/vnd.wap.wml
application/json
adapter
adapter
server
adapterSMS
RPC ??
31. lettuce.it
●
Feature: Manipulate strings
●
In order to have some fun
●
As a programming beginner
●
I want to manipulate strings
●
Scenario: Uppercased strings
●
Given I have the string "lettuce leaves"
●
When I put it in upper case
●
Then I see the string is "LETTUCE LEAVES"
32. lettuce.it
●
>>> @step('I have the string "(.*)"')
●
... def have_the_string(step, string):
●
... world.string = string
●
>>> @step('I put it in upper case')
●
... def put_it_in_upper(step):
●
... world.string = world.string.upper()
●
>>> @step('I see the string is "(.*)"')
●
... def see_the_string_is(step, expected):
●
... assert world.string == expected,
●
... "Got %s" % world.string
33. lettuce.it
●
from lettuce import step
●
from nose.tools import assert_equals
●
@step('some step with "(.*)"'):
●
def some_step(step, from):
●
assert_equals(from, 'expectation')