Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cherrypy
- import sqlite3
- import json
- def wrap_html(function):
- def new_function(*args, **kwargs):
- return """\
- <html><body>
- """ + function(*args, **kwargs) + """
- </body></html>
- """
- return new_function
- def get_db():
- conn = sqlite3.connect("pytalk.sqlite")
- #Turn on autocommit
- conn.isolation_level = None
- c = conn.cursor()
- return c
- class Location():
- @cherrypy.expose
- def index(self, format="text"):
- db = get_db()
- db.execute("select * from locations")
- values = db.fetchall()
- result = { 'count': len(values), 'values': values }
- return json.dumps(result)
- @cherrypy.expose
- def zip(self, zip=None):
- db = get_db()
- db.execute("select * from locations where zip=?", (zip,))
- values = db.fetchall()
- result = { 'count': len(values), 'values': values }
- return json.dumps(result)
- @cherrypy.expose
- @wrap_html
- def pretty(self):
- db = get_db()
- db.execute("select * from locations")
- values = db.fetchall()
- html = "<table>"
- for value in values:
- html += "<tr>"
- for val in value:
- html += "<td>" + str(val) + "</td>"
- html += "</tr>"
- html += "</table>"
- return html
- class HelloWorld():
- location = Location()
- @cherrypy.expose
- @wrap_html
- def index(self):
- return "Hello World!"
- @cherrypy.expose
- @wrap_html
- def hello(self, thing="World"):
- return "Hello " + thing + "!"
- @cherrypy.expose
- def testmethod(self):
- return cherrypy.request.method
- @cherrypy.expose
- def identify(self, name=None):
- if 'POST' == cherrypy.request.method:
- return "You identified as " + name + "."
- else:
- return """
- <form action='identify' method='post'>
- <p>Identify yourself:</p>
- <input type='text' name='name' value=''/>
- <input type='submit' value='Submit'/>
- </form>
- """
- cherrypy.quickstart(HelloWorld())
Add Comment
Please, Sign In to add comment