1、系统自带python 2.7
2、安装MysqL:
点击打开链接
3、安装 MysqLdb 库
yum install MysqL-python
在python客户端导入看是否报错:
import MysqLdb
4、自己先建一个users表:
import MysqLdb
import tornado.ioloop
import tornado.web
cxn = MysqLdb.Connect(host = '127.0.0.1',user = 'root',passwd = '')
class Test(tornado.web.RequestHandler):
def get(self):
result = ""
cur = cxn.cursor()
cur.execute("USE test")
cur.execute("SELECT * FROM users")
for row in cur.fetchall():
result += row.__str__()
print(result)
self.write(result)
class Login(tornado.web.RequestHandler):
def get(self):
userName = self.get_argument('userName','hello')
passWord = self.get_argument('passWord','hello')
sql = "SELECT * FROM users WHERE username='%s'" %(userName)
cur = cxn.cursor()
cur.execute("USE test")
cur.execute(sql)
result = cur.fetchall()
cur.close()
cxn.commit()
if result[0][2] == passWord:
self.write("1")
else:
self.write("0")
application = tornado.web.Application([
(r"/test",Test),(r"/login",Login)
])
if __name__=="__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
cxn.close()