document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import cStringIO as StringIO
  2. import csv
  3. import getpass
  4. import pg8000
  5. import web
  6.  
  7. urls = ((\'/(.*)\', \'Hello\'))
  8.  
  9. web.config.debug = False
  10.  
  11. class Hello:
  12.     def connect(self):
  13.         params = web.input()
  14.  
  15.         try:
  16.             params.host
  17.         except AttributeError:
  18.             params.host = \'\'
  19.  
  20.         try:
  21.             params.user
  22.         except AttributeError:
  23.             params.user = getpass.getuser()
  24.  
  25.         try:
  26.             params.password
  27.         except AttributeError:
  28.             params.password = \'\'
  29.  
  30.         try:
  31.             params.db
  32.         except AttributeError:
  33.             params.db = params.user
  34.  
  35.         self.conn = pg8000.connect(host=params.host, user=params.user, password=params.password, database=params.db)
  36.        
  37.     def GET(self, stuff=None):
  38.         try:
  39.             self.conn
  40.         except AttributeError:
  41.             self.connect()
  42.  
  43.         params = web.input()
  44.         cursor = self.conn.cursor()
  45.         cursor.execute(params.query)
  46.         out = StringIO.StringIO()
  47.         writer = csv.writer(out)
  48.         writer.writerows([r for r in cursor])
  49.         return out.getvalue()
  50.  
  51.     def POST(self, stuff=None):
  52.         try:
  53.             self.conn
  54.         except AttributeError:
  55.             self.connect()
  56.  
  57.         params = web.input()
  58.         cursor = self.conn.cursor()
  59.         cursor.execute(params.query)
  60.         self.conn.commit()
  61.         web.ctx.status = \'201 CREATED\'
  62.  
  63.     def DELETE(self, record):
  64.         params = web.input()
  65.  
  66.         try:
  67.             self.conn
  68.         except AttributeError:
  69.             self.connect()
  70.  
  71.         cursor = self.conn.cursor()
  72.         if not record:
  73.             cursor.execute("DELETE FROM {}".format(params.table))
  74.         else:
  75.             cursor.execute("DELETE FROM {} WHERE id = %s".format(params.table), (record))
  76.         self.conn.commit()
  77.         web.ctx.status = \'204 No Content\'
  78.  
  79.     def HEAD(self, stuff=None):
  80.         try:
  81.             self.conn
  82.         except AttributeError:
  83.             self.connect()
  84.  
  85.         params = web.input()
  86.         cursor = self.conn.cursor()
  87.         cursor.execute(params.query)
  88.         web.header(\'X-Query\',\'{}\'.format(params.query))
  89.         web.header(\'X-Num\', \'{}\'.format(len(list(cursor))))
  90.         web.ctx.status = \'200 OK\'
  91.  
  92. if __name__ == \'__main__\':
  93.     app = web.application(urls, globals())
  94.     app.run()
');