Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

フレームワークなしでWSGIプログラミング

2,823 views

Published on

Published in: Technology, Business
  • Be the first to comment

フレームワークなしでWSGIプログラミング

  1. 1. WSGIマイクロフレームワーク
  2. 2. WSGI!from wsgiref.simple_server import make_server, demo_apphttpd = make_server(, 8000, demo_app)print "Serving HTTP on port 8000..."httpd.serve_forever()
  3. 3. WebOb@wsgifydef application(request): return Response(Hello)http://docs.webob.org
  4. 4. Makoimport osfrom mako.lookup import TemplateLookuphere = os.path.dirname(__file__)templates = TemplateLookup(directories=[os.path.join(here,templates)])tmpl = templates.get_template(index.mak)tmpl.render(request=request)http://www.makotemplates.org/
  5. 5. Routesclass Dispatcher(object): def __init__(self): self.mapper = Mapper() self.controllers = {} def add_route(self, route_name, pattern, controller): self.mapper.connect(route_name, pattern) self.controllers[route_name] = controller
  6. 6. Routes @wsgify def __call__(self, request): matched = self.mapper.routematch(request.path_info,request.method) if not matched: raise HTTPNotFound matchdict, route = matched if route.name not in self.controllers: raise HTTPNotFound request.routes = self.mapper._routenames return self.controllers[route.name](request)
  7. 7. Routesapplication = Dispatcher()application.add_route(index, /, index)application.add_route(hello, /hello, hello)http://routes.groovie.org/
  8. 8. FormEncode Schemaclass HelloSchema(formencode.Schema): name = validators.UnicodeString(not_empty=True)
  9. 9. FormEncode htmlfilldef hello(request): try: params = HelloSchema.to_python(request.params) except formencode.Invalid, e: res = index(request) res.text = htmlfill.render(res.body, request.params,errors=e.error_dict) return reshttp://formencode.org/
  10. 10. putting all togetherhttps://gist.github.com/1175051

×