Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. from datetime import date
  2. import tornado.escape
  3. import tornado.ioloop
  4. import tornado.web
  5. import mysql.connector
  6. import json
  7.  
  8. class UseDatabase:
  9.     def __init__(self, configuration):
  10.         """ Initialisation code which executes the context manager
  11.            is CREATED. """
  12.         self.host = configuration['DB_HOST']
  13.         self.user = configuration['DB_USER']
  14.         self.passwd = configuration['DB_PASSWD']
  15.         self.db = configuration['DB']
  16.  
  17.     def __enter__(self):
  18.         """ Set-up code which executes BEFORE the body of the
  19.            with statement. """
  20.         self.conn = mysql.connector.connect(host=self.host,
  21.                                             user=self.user,
  22.                                             password=self.passwd,
  23.                                             database=self.db,)
  24.         self.cursor = self.conn.cursor()
  25.         return(self.cursor)
  26.  
  27.     def __exit__(self, exc_type, exv_value, exc_traceback):
  28.         """ Tear-down code with executes AFTER the body of the
  29.            with statement.
  30.            The three extra parameters to __exit__() contain information
  31.            related to any exception which may have occurred while running
  32.            the body of the with statement. """
  33.         self.cursor.close()
  34.         self.conn.commit()
  35.         self.conn.close()
  36.  
  37. config = {'DB_HOST' : 'localhost','DB_USER' : 'root' , 'DB_PASSWD' : '******', 'DB' : 'db'}
  38.  
  39. class VersionHandler(tornado.web.RequestHandler):
  40.     def get(self):
  41.         response = { 'version': '3.5.1',
  42.                      'last_build':  date.today().isoformat() }
  43.         self.write(response)
  44.  
  45. class SignIn(tornado.web.RequestHandler):
  46.     def get(self):
  47.         username = self.get_argument('name')
  48.         with UseDatabase(config) as cursor:
  49.             SQL = '''select * from users where username = "%s" Limit 1'''% (username)
  50.             cursor.execute(SQL)
  51.             userData = cursor.fetchone()
  52.         message = {}
  53.         message["userData"] = userData
  54.         json.dumps(message)
  55.         self.write(message)
  56.     def post(self):
  57.         username = self.get_argument('name')
  58.         password = self.get_argument('password')
  59.         with UseDatabase(config) as cursor:
  60.             SQL = '''INSERT INTO users (username , password) VALUES ( "%s" , "%s")'''% (username,password)
  61.             cursor.execute(SQL)
  62.         response = "{ 'create user with name : '" + username + " and password : " + password + "}"
  63.         self.write(response)
  64.  
  65. application = tornado.web.Application([
  66.     (r"/signin", SignIn),
  67.     (r"/version", VersionHandler)
  68. ])
  69.  
  70. if __name__ == "__main__":
  71.     application.listen(443)
  72.     tornado.ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement