python web with bottle


/ Published in: Python
Save to your folder(s)

**work in progress**


Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import bottle as web # http://bottle.paws.de/page/docs
  5. DEV=True
  6. web.debug(DEV)
  7.  
  8.  
  9. @web.route("/static/:path")
  10. def static(path):
  11. web.send_file(path,root="static")
  12.  
  13. @web.route('/:name#.*#')
  14. def index(name):
  15. name = name or "unknown"
  16. web.response.content_type = 'text/html; charset=utf-8'
  17. web.response.COOKIES['kiki'] = name
  18. return """hello %(name)s
  19. <a href='/do/show?a=1&a=2'>show</a>""" % locals()
  20.  
  21. @web.route('/do/:cmd?')
  22. def cmd(cmd):
  23. if cmd=="show":
  24. yield "<li>cookies : %s</li>"% str(web.request.COOKIES)
  25. yield "<li>get : %s</li>"% str(web.request.GET)
  26. yield "<li>post : %s</li>"% str(web.request.POST)
  27. else:
  28. web.redirect("/") #don't work with yield now ;-(
  29.  
  30. @web.route('/.*')
  31. def fallback():
  32. yield "my 404"
  33. #~ web.abort(404, "Not Found")
  34.  
  35. @web.error(500) # don't work for me ?!?
  36. def fallback500(err):
  37. return "my error:"+str(err)
  38.  
  39. def main(useGae=False):
  40. if useGae:
  41. from google.appengine.ext.webapp import util
  42. util.run_wsgi_app(web.default_app())
  43. else:
  44. web.run(reloader=DEV)
  45.  
  46. if __name__=="__main__":
  47. main()

URL: http://bottle.paws.de/page/docs

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.