Python 登陆接口小程序V1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 登陆接口小程序V1相关的知识,希望对你有一定的参考价值。
Python 登陆接口小程序版本V1
Python
#!/usr/bin/env python #_*_coding:utf-8 _*_ __author__ = ‘gaogd‘ import mysqldb as mysql import datetime class Authon(object): def __init__(self): self.db = mysql.connect(user="root", passwd="[email protected]", db="logintest", host="192.168.10.12") # 数据库连接信息 self.db.autocommit(True) self.cur = self.db.cursor() self.cur.execute(‘set names utf8‘) def Login(self): username = raw_input(u‘请输入你的用户名:‘) err_count = 0 Flag = True while Flag: password = raw_input(u‘请输入密码:‘) if username.strip() == ‘‘: print u‘用户名不能为空!‘ # sql = select user,passwd from loginuser where user=‘gao3‘and passwd=‘gao3‘; select_user_passwd_sql = "select user,passwd from loginuser where user=‘%s‘and passwd=‘%s‘;" % (username,password) self.cur.execute(select_user_passwd_sql) select_user_passwd = self.cur.fetchall() if len(select_user_passwd) == 0: print u‘用户名或密码不正确!!‘ err_count += 1 if err_count == 3: print u‘错误输入密码超过三次,程序自动退出!!‘ Flag = False else: err_count = 0 print ‘欢迎您来%s到Python家族!!‘ % username return True def Register(self): username = raw_input(u‘请输入你要申请的用户名:‘) password_one = raw_input(u‘请输入密码:‘) password_two = raw_input(u‘请再次输入密码:‘) addtime = datetime.datetime.now() if username.strip() == ‘‘: print u‘用户名不能为空!‘ if not username.isalnum(): print u‘用户名不能包含特殊字符‘ return False if password_one.strip() == ‘‘: print u‘密码不能为空!‘ return False if not password_one.strip() == password_two.strip(): print u‘你两次输入的密码不一致‘ return False ## sql = insert into loginuser(user,passwd,err_count,time) values(‘gaogd‘,‘gaogd‘,‘0‘,‘2016-09-02 14:40:22‘); create_user_sql = "insert into loginuser(user,passwd,err_count,time) values(‘%s‘,‘%s‘,‘%s‘,‘%s‘)" % (username,password_one,0,addtime) try: print create_user_sql self.cur.execute(create_user_sql) except Exception as e: print e print u‘创建用户失败!!‘ return False print u‘创建用户成功:‘,username def Deleuser(self): username = raw_input(u‘请输入你要删除的用户名:‘) password = raw_input(u‘请输入密码:‘) if username.strip() == ‘‘: print u‘用户名不能为空!‘ select_user_sql = "select user from loginuser;" self.cur.execute(select_user_sql) select_user = self.cur.fetchall() Flag = False for n in range(len(select_user)): if username == select_user[n][0]: # username,select_user[n][0] Flag = True if Flag : #select passwd from loginuser where user=‘gao‘;v select_passwd_sql = "select passwd from loginuser where user=‘%s‘;" % username self.cur.execute(select_passwd_sql) select_passwd = self.cur.fetchall() if password.strip() == select_passwd[0][0].strip(): ##删除用户 ##DELETE FROM loginuser WHERE user=‘gao1‘; select_deluser_sql = "DELETE FROM loginuser WHERE user=‘%s‘;" % username self.cur.execute(select_deluser_sql) print ‘已经成功删除%s用户‘ % username else: print ‘用户名或密码不正确!!‘ def SelectFunction(): content=‘‘‘ 1.登陆Python家族 2.注册用户 3.删除用户 ‘‘‘ print content num = raw_input(u‘请输入您的选择:‘) if num.strip() == ‘1‘: authon.Login() return True if num.strip() == ‘2‘: authon.Register() return True if num.strip() == ‘3‘: authon.Deleuser() return True print u‘您的输入有误!!请输入您需要的功能对应的序号‘ if __name__ == "__main__": #pass authon = Authon() #authon.Register() #authon.Login() #authon.Deleuser() SelectFunction() """ 数据数据库以及对应的表 create database logintest ; use logintest; CREATE TABLE `loginuser` ( `user` varchar(64) primary key, `passwd` varchar(128) , `err_count` int , `time` datetime ) ENGINE=InnoDB DEFAULT CHARSET=utf8; mysql> desc loginuser; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | user | varchar(64) | NO | PRI | NULL | | | passwd | varchar(128) | YES | | NULL | | | err_count | int(11) | YES | | NULL | | | time | datetime | YES | | NULL | | +-----------+--------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> 注册用户 登陆用户 删除用户 """
本文出自 “奋斗吧” 博客,请务必保留此出处http://lvnian.blog.51cto.com/7155281/1845676
以上是关于Python 登陆接口小程序V1的主要内容,如果未能解决你的问题,请参考以下文章