@ranger-01
2018-03-18T16:19:56.000000Z
字数 1549
阅读 899
web_server
在上一篇文章中,我们知道web工作刘如下:
the web client <-> the web server <-> the socket <-> (uwsgi protocol)uWSGI(WSGI) <-> Python
并用例子说明了,application server(uWSGI)要做的事情:
从输入中获取数据构造env字典(handle_one_request, parse_request
),里面一般包括:
根据application返回的output,构造http response
start_response
构造response header URL mapping
URL map详情
ROUTES_SIMPLE = {
'GET': {
'URL': handler
},
'POST': {
'URL': handler
}
}
ROUTES_REGEXP = {
'GET': [
[Pattern object, handler],
[Pattern object, handler]
]
'POST': [
[Pattern object, handler],
[Pattern object, handler]
]
}
URL match
def match_url(url, method='GET'):
url = '/' + url.strip().lstrip("/")
# Search for static routes first
route = ROUTES_SIMPLE.get(method,{}).get(url,None)
if route:
return (route, {})
# Now search regexp routes
routes = ROUTES_REGEXP.get(method,[])
for i in xrange(len(routes)):
match = routes[i][0].match(url)
if match:
handler = routes[i][9]
if i > 0 and OPTIMIZER and random.random() <= 0.001:
# Every 1000 requests, we swap the matching route with its predecessor.
# Frequently used routes will slowly wander up the list.
routes[i-1], routes[i] = routes[i], routes[i-1]
return (handler, match.groupdict())
return (None, None)