Guest User

Untitled

a guest
Jan 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import cherrypy
  2. import sqlite3
  3. import json
  4.  
  5. def wrap_html(function):
  6. def new_function(*args, **kwargs):
  7. return """\
  8. <html><body>
  9. """ + function(*args, **kwargs) + """
  10. </body></html>
  11. """
  12. return new_function
  13.  
  14. def get_db():
  15. conn = sqlite3.connect("pytalk.sqlite")
  16. #Turn on autocommit
  17. conn.isolation_level = None
  18. c = conn.cursor()
  19. return c
  20.  
  21.  
  22. class Location():
  23. @cherrypy.expose
  24. def index(self, format="text"):
  25. db = get_db()
  26. db.execute("select * from locations")
  27. values = db.fetchall()
  28. result = { 'count': len(values), 'values': values }
  29. return json.dumps(result)
  30.  
  31. @cherrypy.expose
  32. def zip(self, zip=None):
  33. db = get_db()
  34. db.execute("select * from locations where zip=?", (zip,))
  35. values = db.fetchall()
  36. result = { 'count': len(values), 'values': values }
  37. return json.dumps(result)
  38.  
  39. @cherrypy.expose
  40. @wrap_html
  41. def pretty(self):
  42. db = get_db()
  43. db.execute("select * from locations")
  44. values = db.fetchall()
  45. html = "<table>"
  46. for value in values:
  47. html += "<tr>"
  48. for val in value:
  49. html += "<td>" + str(val) + "</td>"
  50. html += "</tr>"
  51. html += "</table>"
  52. return html
  53.  
  54.  
  55. class HelloWorld():
  56. location = Location()
  57.  
  58. @cherrypy.expose
  59. @wrap_html
  60. def index(self):
  61. return "Hello World!"
  62.  
  63. @cherrypy.expose
  64. @wrap_html
  65. def hello(self, thing="World"):
  66. return "Hello " + thing + "!"
  67.  
  68. @cherrypy.expose
  69. def testmethod(self):
  70. return cherrypy.request.method
  71.  
  72. @cherrypy.expose
  73. def identify(self, name=None):
  74. if 'POST' == cherrypy.request.method:
  75. return "You identified as " + name + "."
  76. else:
  77. return """
  78. <form action='identify' method='post'>
  79. <p>Identify yourself:</p>
  80. <input type='text' name='name' value=''/>
  81. <input type='submit' value='Submit'/>
  82. </form>
  83. """
  84.  
  85. cherrypy.quickstart(HelloWorld())
Add Comment
Please, Sign In to add comment