天水 网站建设招聘,网页设计作业怎么打包,旅游网站内容规划特点,万网域名怎样把淘宝网站加进去在正式开始Web开发前#xff0c;我们需要编写一个Web框架。
为什么不选择一个现成的Web框架而是自己从头开发呢#xff1f;我们来考察一下现有的流行的Web框架#xff1a;
Django#xff1a;一站式开发框架#xff0c;但不利于定制化#xff1b;
web.py#xff1a;使用类…在正式开始Web开发前我们需要编写一个Web框架。
为什么不选择一个现成的Web框架而是自己从头开发呢我们来考察一下现有的流行的Web框架
Django一站式开发框架但不利于定制化
web.py使用类而不是更简单的函数来处理URL并且URL映射是单独配置的
Flask使用decorator的URL路由不错但框架对应用程序的代码入侵太强
bottle缺少根据URL模式进行拦截的功能不利于做权限检查。
所以我们综合几种框架的优点设计一个简单、灵活、入侵性极小的Web框架。
设计Web框架
一个简单的URL框架应该允许以decorator方式直接把URL映射到函数上
# 首页:
get(/)
def index():
return
Index page
# 带参数的URL:
get(/user/:id)
def show_user(id):
user User.get(id)
return hello, %s % user.name
有没有decorator不改变函数行为也就是说Web框架的API入侵性很小你可以直接测试函数show_user(id)而不需要启动Web服务器。
函数可以返回str、unicode以及iterator这些数据可以直接作为字符串返回给浏览器。
其次Web框架要支持URL拦截器这样我们就可以根据URL做权限检查
interceptor(/manage/)
def check_manage_url(next):
if current_user.isAdmin():
return next()
else:
raise seeother(/signin)
拦截器接受一个next函数这样一个拦截器可以决定调用next()继续处理请求还是直接返回。
为了支持MVCWeb框架需要支持模板但是我们不限定使用哪一种模板可以选择jinja2也可以选择mako、Cheetah等等。
要统一模板的接口函数可以返回dict并配合view来渲染模板
view(index.html)
get(/)
def index():
return dict(blogsget_recent_blogs(), userget_current_user())
如果需要从form表单或者URL的querystring获取用户输入的数据就需要访问request对象如果要设置特定的Content-Type、设置Cookie等就需要访问response对象。request和response对象应该从一个唯一的ThreadLocal中获取
get(/test)
def test():
input_data ctx.request.input()
ctx.response.content_type text/plain
ctx.response.set_cookie(name, value, expires3600)
return result
最后如果需要重定向、或者返回一个HTTP错误码最好的方法是直接抛出异常例如重定向到登陆页
raise seeother(/signin)
返回404错误
raise notfound()
基于以上接口我们就可以实现Web框架了。
实现Web框架
最基本的几个对象如下
# transwarp/web.py
# 全局ThreadLocal对象
ctx threading.local()
# HTTP错误类:
class HttpError(Exception):
pass
# request对象:
class Request(object):
# 根据key返回value:
def get(self, key, defaultNone):
pass
# 返回key-value的dict:
def input(self):
pass
# 返回URL的path:
property
def path_info(self):
pass
# 返回HTTP Headers:
property
def headers(self):
pass
# 根据key返回Cookie value:
def cookie(self, name, defaultNone):
pass
# response对象:
class Response(object):
# 设置header:
def set_header(self, key, value):
pass
# 设置Cookie:
def set_cookie(self, name, value, max_ageNone, expiresNone, path/):
pass
# 设置status:
property
def status(self):
pass
status.setter
def status(self, value):
pass
# 定义GET:
def get(path):
pass
# 定义POST:
def post(path):
pass
# 定义模板:
def view(path):
pass
# 定义拦截器:
def interceptor(pattern):
pass
# 定义模板引擎:
class TemplateEngine(object):
def __call__(self, path, model):
pass
# 缺省使用jinja2:
class Jinja2TemplateEngine(TemplateEngine):
def __init__(self, templ_dir, **kw):
from jinja2 import Environment, FileSystemLoader
self._env Environment(loaderFileSystemLoader(templ_dir), **kw)
def __call__(self, path, model):
return self._env.get_template(path).render(**model).encode(utf-8)
把上面的定义填充完毕我们就只剩下一件事情定义全局WSGIApplication的类实现WSGI接口然后通过配置启动就完成了整个Web框架的工作。
设计WSGIApplication要充分考虑开发模式Development Mode和产品模式Production Mode的区分。在产品模式下WSGIApplication需要直接提供WSGI接口给服务器让服务器调用该接口而在开发模式下我们更希望能通过app.run()直接启动服务器进行开发调试
wsgi WSGIApplication()
if __name__ __main__:
wsgi.run()
else:
application wsgi.get_wsgi_application()
因此WSGIApplication定义如下
class WSGIApplication(object):
def __init__(self, document_rootNone, **kw):
pass
# 添加一个URL定义:
def add_url(self, func):
pass
# 添加一个Interceptor定义:
def add_interceptor(self, func):
pass
# 设置TemplateEngine:
property
def template_engine(self):
pass
template_engine.setter
def template_engine(self, engine):
pass
# 返回WSGI处理函数:
def get_wsgi_application(self):
def wsgi(env, start_response):
pass
return wsgi
# 开发模式下直接启动服务器:
def run(self, port9000, host127.0.0.1):
from wsgiref.simple_server import make_server
server make_server(host, port, self.get_wsgi_application())
server.serve_forever()
Try
把WSGIApplication类填充完毕我们就得到了一个完整的Web框架。