zoukankan      html  css  js  c++  java
  • pymysql实现用户注册登录功能

      1 #-*- coding:utf8 -*-  
    2
    import pymysql 3 4 def print_tips(str_info): #blue 5 print '33[1;34m%s33[0m'%str_info 6 7 def print_info(str_info): #green 8 print '33[1;32m%s33[0m'%str_info 9 10 def print_warn(str_info): #yellow 11 print '33[1;33m%s33[0m'%str_info 12 13 def print_err(str_info): #red 14 print '33[1;31m%s33[0m'%str_info 15 16 17 ###数据库连接 18 def func_connect_mysql(host,user,db_name,db_pwd): 19 message = { 20 "host":host, 21 "user":user, 22 "password":db_pwd, 23 "database":db_name, 24 "autocommit":True 25 } 26 db = pymysql.connect(**message) 27 return db 28 29 30 31 # 4 success 32 # 1 goto 33 ###登陆函数 34 def func_login(cursor): 35 print('33[1;35mLogin33[0m') 36 37 flag_login = 0 38 flag_registration = 0 39 flag_find_pwd = 0 40 41 while True: 42 user_name = raw_input("33[1;32musername:33[0m") 43 #print type(user_name) 44 user_pwd = input("33[1;32mpassword:33[0m") 45 sql = "select * from users where username = '%s' and password = '%s'"%(user_name,user_pwd) 46 result = cursor.execute(sql) 47 48 if result: 49 print_tips("welcome %s,login successful"%user_name) 50 flag_login = 4 51 break 52 else: 53 sql_2 = "select * from users where username = '%s'"%user_name 54 result2 = cursor.execute(sql_2) 55 if result2: 56 print_err("password error!") 57 58 while True: 59 print_warn("if you want to try again,enter 1") 60 print_warn("if you want to find password,enter 2") 61 num = input("33[1;32m1/2:33[0m") 62 if num == 1: 63 flag_login = 1 64 break 65 elif num == 2: 66 flag_login = 2 67 break 68 else: 69 print_err("Input error!please input 1 or 2") 70 continue 71 72 if flag_login == 1: 73 continue 74 else: 75 flag_find_pwd = 1 76 break 77 78 else: 79 print_err("user not exists!") 80 while True: 81 print_warn("if you want to try again,enter 1") 82 print_warn("if you want to Registration,enter 2") 83 num = input("33[1;32m1/2:33[0m") 84 if num == 1: 85 flag_login = 1 86 break 87 elif num == 2: 88 flag_registration = 1 89 break 90 else: 91 print_err("please enter 1 or 2") 92 continue 93 break 94 95 return flag_login,flag_registration,flag_find_pwd,user_name 96 97 ###找回密码函数 98 def func_find_pwd(user_name,cursor): 99 print('33[1;35mFind password33[0m') 100 flag_login = 0 101 flag_find_pwd = 0 102 flag_registration = 0 103 password = "" 104 105 while True: 106 phone_number = input('33[1;32mplease Enter your phone number:33[0m') 107 sql = "select password from users where username = '%s' and phone = '%s'"%(user_name,phone_number) 108 result = cursor.execute(sql) 109 if result: 110 password = cursor.fetchone() 111 flag_login = 1 112 break 113 else: 114 print_err("phone number error!") 115 while True: 116 print_warn("Enter 1 try again Enter 2 go to Registration") 117 num = input('33[1;32m1/2:33[0m') 118 if num == 1: 119 flag_find_pwd = 1 120 break 121 elif num == 2: 122 flag_find_pwd = 2 123 break 124 else: 125 continue 126 if flag_find_pwd == 1: 127 continue 128 elif flag_find_pwd == 2: 129 flag_registration = 1 130 break 131 132 return flag_login,flag_registration,password 133 134 135 ###注册函数 136 def func_registration(db,cursor): 137 #print('33[1;35mRegistration33[0m') 138 flag_login = 0 139 while True: 140 #username: 141 print('33[1;35mRegistration33[0m') 142 username = raw_input('33[1;32mEnter username:33[0m') 143 sql1 = "select * from users where username = '%s'"%username 144 result1 = cursor.execute(sql1) 145 if result1: 146 print_err("user exists!") 147 print_warn("Enter 1 try again Enter 2 go to login") 148 num = input('33[1;32m1/2:33[0m') 149 if num == 1: 150 continue 151 else: 152 flag_login = 1 153 break 154 155 #password 156 password = raw_input('33[1;32mEnter password:33[0m') 157 password1 = raw_input('33[1;32mEnter password again:33[0m') 158 if password1 != password: 159 print_err("Passwords are inconsistent!") 160 continue 161 else: 162 163 #phone 164 phone_number = input('33[1;32mEnter phone:33[0m') 165 166 #registration 167 sql = "insert into users(username,password,phone) values ('%s','%s','%s');"%(username,password,phone_number) 168 result = cursor.execute(sql) 169 #db.commit() 170 data = cursor.fetchall() 171 if result: 172 print_tips("registration successful!") 173 flag_login = 1 174 break 175 else: 176 print_err("registration fail!") 177 print warn("Try again ,Y/N") 178 num = input("33[1;32mY/N:33[0m") 179 if num =="Y": 180 continue 181 else: 182 break 183 return flag_login 184 185 186 ###修改用户信息函数 187 def change_message(num,id_primary,cursor): 188 flag = False 189 flag_run = False 190 if num == 1: 191 print("33[1;35malter password33[0m") 192 print_warn("please enter your new password") 193 new_password = raw_input("33[1;32mnew password:33[0m") 194 sql1 = "select password from users where id = '%d'"%id_primary 195 row = cursor.execute(sql1) 196 old_password = cursor.fetchone()[0] 197 if old_password != new_password: 198 sql = "update users set password = '%s' where id = '%d'"%(new_password,id_primary) 199 flag_run = True 200 else: 201 print_err("new password = old password!") 202 203 elif num == 2: 204 print("33[1;35malter username33[0m") 205 print_warn("please enter your new username") 206 new_username = raw_input("33[1;32mnew username:33[0m") 207 sql1 = "select username from users where id = '%d'"%id_primary 208 row = cursor.execute(sql1) 209 old_username = cursor.fetchone()[0] 210 if old_username != new_username: 211 sql = "update users set username = '%s' where id = '%d'"%(new_username,id_primary) 212 flag_run = True 213 else: 214 print_err("new username = old username!") 215 elif num == 3: 216 print("33[1;35malter phone33[0m") 217 print_warn("please enter your new phone") 218 new_phone = raw_input("33[1;32mnew phone:33[0m") 219 sql1 = "select phone from users where id = '%d'"%id_primary 220 row = cursor.execute(sql1) 221 old_phone = cursor.fetchone()[0] 222 if old_phone != new_phone: 223 sql = "update users set phone = '%s' where id = '%d'"%(new_phone,id_primary) 224 flag_run = True 225 else: 226 print_err("new phone = old phone!") 227 elif num == 4: 228 print("33[1;35mdelete account33[0m") 229 while True: 230 print_warn("confirm deletion,enter 1 think again,enter 2") 231 num_delete = input("33[1;32m1/2:33[0m") 232 if num_delete == 1: 233 sql = "delete from users where id = '%d'"%id_primary 234 flag_run = True 235 break 236 elif num_delete == 2: 237 break 238 else: 239 print_err("please enter 1/2!") 240 continue 241 if flag_run: 242 result = cursor.execute(sql) 243 if result: 244 flag = True 245 return flag 246 247 248 249 ###主流程函数 250 def func_main(db): 251 cursor = db.cursor() 252 cursor.execute("create table if not exists users(id int primary key auto_increment,username char(10) unique,password char(20),phone char(11))") 253 while True: 254 login_flag_login = -1 255 login_flag_registration = -1 256 login_flag_find_pwd = -1 257 pwd_flag_login = -1 258 pwd_flag_registration = -1 259 password = "" 260 registration_flag_login = -1 261 id_primary = -1 262 print_warn("if you want to login,enter 1") 263 print_warn("if you want to registration,enter 2") 264 print_warn("if you want to quit,enter 5") 265 num_login = input("33[1;32m1/2/5:33[0m") 266 if num_login != 1 and num_login != 2 and num_login != 5: 267 print_err("please enter 1 or 2 or 5!") 268 continue 269 if num_login == 5: 270 break 271 elif num_login == 1: 272 login_flag_login,login_flag_registration,login_flag_find_pwd,user_name = func_login(cursor) 273 if login_flag_login == 4: 274 while True: 275 print_warn("if you want to alter password,enter 1") 276 print_warn("if you want to alter username,enter 2") 277 print_warn("if you want to alter phone,enter 3") 278 print_warn("if you want to delete your acount,enter 4") 279 print_warn("if you want to quit,enter 5") 280 num = input('33[1;32m1/2/3:33[0m') 281 sql = "select id from users where username = '%s'"%user_name 282 row = cursor.execute(sql) 283 if row: 284 id_primary = cursor.fetchone()[0] 285 if num == 1 or num == 2 or num == 3: 286 if id_primary != -1: 287 flag = change_message(num,id_primary,cursor) 288 if flag: 289 print_tips("alter success!") 290 break 291 else: 292 print_err("sorry,alter fail,please try again") 293 continue 294 elif num == 4: 295 if id_primary != -1: 296 flag = change_message(num,id_primary,cursor) 297 if flag: 298 print_tips("delete success!") 299 num_login == 1 300 break 301 else: 302 print_err("sorry,delete fail,please try again") 303 continue 304 elif num == 5: 305 break 306 else: 307 print_err("please enter 1/2/3/4") 308 continue 309 if login_flag_find_pwd == 1: 310 pwd_flag_login,pwd_flag_registration,password = func_find_pwd(user_name,cursor) 311 if pwd_flag_login == 1 and password != "": 312 print_tips("password:"+password[0]) 313 continue 314 if pwd_flag_registration == 1 or login_flag_registration == 1 or num_login == 2: 315 registration_flag_login = func_registration(db,cursor) 316 if registration_flag_login == 1: 317 continue 318 else: 319 print_err("Registration Failed!") 320 321 322 if __name__=='__main__': 323 db = func_connect_mysql("localhost","root","Student_base","123456") 324 func_main(db)
  • 相关阅读:
    Android开发进阶 -- 通用适配器 CommonAdapter
    如何巧妙地在基于 TCP Socket 的应用中实现用户注册功能?
    如何让基于.NET 2.0的应用在高版本的系统上运行?
    即时通信系统中实现全局系统通知,并与Web后台集成【附C#开源即时通讯系统(支持广域网)——QQ高仿版IM最新源码】
    即时通信系统中实现聊天消息加密,让通信更安全【低调赠送:C#开源即时通讯系统(支持广域网)——GGTalk4.5 最新源码】
    程序员的那些事儿 -- 高级程序员买衣服
    程序员的那些事儿 -- 皆大欢喜的加薪
    GGTalk即时通讯系统(支持广域网)终于有移动端了!(技术原理、实现、源码)
    如何做到在虚拟数据库和真实数据库之间自由切换?【低调赠送:QQ高仿版GG 4.4 最新源码】
    如何实现:录制视频聊天的全过程? 【低调赠送:QQ高仿版GG 4.3 最新源码】
  • 原文地址:https://www.cnblogs.com/chunqiu666/p/12752454.html
Copyright © 2011-2022 走看看