def login(self): #登录 self.userphone = input("手动输入手机号码\n") # 输入登录信息 self.userpassword = getpass.getpass() user.jiami() #加密处理后与数据库对接 # 需要连接数据库 connect_obj = Mysqlpython("database_user_first_info") #数据库中登录信息表 sql = "select userpassword from user_info \ where userphonename = %s ;" % self.userphone #match是通过语音输入的手机号码匹配出来的密码 match = connect_obj.all(sql) if not match: print("用户未注册或匹配错误") key = input("重新输入还是进行忘记操作\n1.登录 2.注册 3.忘记密码 0.主页面\n") #点击页面显示按钮选择 if key == '1': user.login() elif key == '3': user.forget_password() elif key == '0': user.showmain() elif key == '2': user.set_in() elif match[0][0] == self.userpassword: print("登录成功")
def forget_password(self): #忘记密码 #调取通过手机短信验证码方式验证,通过进行处理 self.userphone = input("页面上输入手机号码\n") connect_obj = Mysqlpython("database_user_first_info") #数据库中登录信息表 sql = "select * from user_info \ where userphonename = %s" % self.userphone #匹配表内是否已经注册过 match = connect_obj.all(sql) if not match: print(" 手机号码没有注册或者手机号码长度有误\ \n请核实后操作") user.forget_password() else: user.phone_check() #发送短信验证码验证 key = input("请输入你收到的验证码") if key == self.massage_num: #进行验证 password1 = input("通过页面重新设置密码\n") password2 = input("通过页面再次输入密码\n") if password1 == password2 and len(password1) >= 6: self.userpassword = password1 user.jiami() sql = "update user_info set userpassword = '******' where userphonename = '%s';" % ( self.userpassword, self.userphone) match = connect_obj.update_ziduan(sql) print("重置成功\n将返回主页面登录") else: print("两次密码输入不一致\n请重新操作") user.forget_password() else: print("验证码不正确请确认") user.forget_password()
def set_in(self): #注册 self.userphone = input("通过页面点击输入您需要注册的手机号码\n") password1 = input("通过页面点击设置第一次密码\n注意:密码长度不能少于6位,最大20位\n") password2 = input("通过页面点击设置第二次密码\n") if password1 == password2 and len(password1) >= 6 and len( self.userphone) == 11: connect_obj = Mysqlpython("database_user_first_info") #数据库中登录信息表 sql = "select * from user_info \ where userphonename = %s" % self.userphone #匹配表内是否已经注册过 match = connect_obj.all(sql) if not match: user.phone_check() key = input("首次注册需要验证您的手机号码\n请输入您收到的验证码验证\n") if key == self.massage_num: #验证发送的短信验证码是否正确 print("注册成功") key = input("是否进行邮箱验证以便后续使用?\n1.是 2.暂时不需要\n") if key == '1': s = send_check_email() self.emailaddress = s.mail() if key != '1': pass self.userpassword = password1 user.jiami() sql = "insert into user_info values (0,'%s','%s','%s');" % ( self.userphone, self.userpassword, self.emailaddress) connect_obj.zhixing(sql) #将注册信息添加入数据库表 user.login() #返回登录界面重新登录 else: print("输入验证码不正确\n返回重新注册") user.set_in() else: print("帐号已存在") key = input("返回登录界面或忘记密码\n 0返回主页面 1重新登录 2重新注册\n") if key == '0': user.showmain() if key == '1': user.login() if key == '2': user.set_in() key = input("两次输入密码不一致或手机号码长度有误\n 0返回主页面 1登录 2重新注册\n") if key == '0': user.showmain() if key == '1': user.login() if key == '2': user.set_in()
from mysqlpython import Mysqlpython from hashlib import sha1 u_name = input('请输入用户名:') p_word = input('请输入密码:') # 用 sha1 给 p_word 加密 s1 = sha1() # 创建sha1加密对象 s1.update(p_word.encode('utf8')) p_word2 = s1.hexdigest() # 返回16进制加密结果 sqlh = Mysqlpython('country') select = 'select password from user where\ username=%s;' date = sqlh.all(select, [u_name]) if len(date) == 0: print('用户名不存在') elif date[0][0] == p_word2: print('登录成功') else: print('密码错误')
from mysqlpython import Mysqlpython a=Mysqlpython("db5") # a.zhixing("insert into t2(name,score) values('奥特曼',100)") # a.zhixing("update t2 set score = 99.99") print(a.many("select * from t2",4)) print(a.all("select * from t2",))
from mysqlpython import Mysqlpython # 创建数据库连接对象 sqlh = Mysqlpython("db4") # sql_update = "update sheng set s_name='辽宁省' \ # where s_name='云南省';" # sqlh.zhixing(sql_update) sql_select = "select * from sheng where id=%s;" data = sqlh.all(sql_select, [1]) print(data)
class TftpServer(object): def __init__(self, connfd): self.connfd = connfd self.db = Mysqlpython("tftp") def do_login(self, name, passwd): '''登录''' select = "select name,passwd from user where name=%s" result = self.db.all(select, [name]) if len(result) == 0: self.connfd.send(b"nameError") else: if result[0][1] == passwd: self.connfd.send(b"OK") else: self.connfd.send(b"passwdError") def do_register(self, name, passwd): '''注册''' select = "select name from user where name=%s" result = self.db.all(select, [name]) if len(result) == 0: insert = "insert into user(name,passwd) values(%s,%s)" result = self.db.zhixing(insert, [name, passwd]) if result == "OK": self.connfd.send(b"OK") # 注册成功时在路径下创建该用户的文件夹 os.mkdir(FAIL_PATH + name) else: print(result) self.connfd.send(b"failed") else: self.connfd.send(b"exist") def do_put(self, name, filename): '''客户端上传''' with open(FAIL_PATH + name + '/' + filename, "wb") as f: while True: data = self.connfd.recv(4096) if data == b"##*##": data = self.connfd.send(b"OVER") break f.write(data) # 将文件名大小存入数据库 try: remo = "delete from document where name=%s and filename=%s" self.db.zhixing(remo, [name, filename]) insert = "insert into document(name,filename,filesize) values(%s,%s,%s)" filesize = os.path.getsize(FAIL_PATH + name + '/' + filename) self.db.zhixing(insert, [name, filename, filesize]) except Exception as e: print(e) def decide(self, name, filename): # 判断文件是否存在 filename = filename.split("#*#") decide_file = "" for x in filename: if x != "" and x in os.listdir(FAIL_PATH + name): decide_file += x + "#*#" if decide_file: self.connfd.send(decide_file.encode()) else: self.connfd.send(b"None") def showfile(self, name): # 返回服务器该用户所有的文件名和文件大小 files = "" for x in os.listdir(FAIL_PATH + name): files += x + "#*#" if files: self.connfd.send(files.encode()) else: self.connfd.send(b"None") def remove(self, name, filename): # 删除文件 if filename in os.listdir(FAIL_PATH + name): os.remove(FAIL_PATH + name + '/' + filename) self.connfd.send(b"OK") remo = "delete from document where name=%s and filename=%s" self.db.zhixing(remo, [name, filename]) else: self.connfd.send(b"None") def do_get(self, name, filename): # 客户端下载 if filename in os.listdir(FAIL_PATH + name): self.connfd.send(b"OK") time.sleep(0.1) with open(FAIL_PATH + name + '/' + filename, "rb") as f: while True: data = f.read(4096) if not data: time.sleep(0.1) self.connfd.send(b"##*##") break self.connfd.send(data) else: self.connfd.send(b"failed")
from mysqlpython import Mysqlpython # 创建对象 sqlh = Mysqlpython("db5") sqlh.zhixing('update t1 set score=100') r = sqlh.all('select * from t1') print(r)
from hashlib import sha1 menu = """1.注册 2.登录 q.退出 请选择(1/2/q):""" #到数据库user表中去查 sqlh = Mysqlpython("db5") while 1: choice = input(menu) if choice.strip() == "1": # 先让用户输入注册的用户名 uname = input("请输入用户名:") sele = "select username from user where username = %s" r = sqlh.all(sele, [uname]) if r: print("用户名已存在") continue else: pwd1 = input("请输入密码:") pwd2 = input("请再次输入密码:") #密码一致,注册成功 if pwd1 == pwd2: #对密码进行加密,存入数据库 s = sha1() #创建sha1加密对象 s.update(pwd1.encode("utf8")) #不能对字符串加密 要把字符串转为字节流 pwd = s.hexdigest() #返回十六进制加密结果 ins = "insert into user values(%s,%s)"
from mysqlpython import Mysqlpython from hashlib import sha1 uname = input("请输入用户名:") pwd = input("请输入密码:") # 用sha1给pwd加密 s1 = sha1() # 创建sha1加密对象 s1.update(pwd.encode("utf8")) # 指定编码 pwd2 = s1.hexdigest() # 返回16进制加密结果 sqlh = Mysqlpython("db4") select = "select password from user where \ username=%s;" result = sqlh.all(select, [uname]) # print(result) # (('7c4a8d09ca3762af61e59520943dc26494f8941b',),) if len(result) == 0: print("用户名不存在") elif result[0][0] == pwd2: print("登录成功") else: print("密码错误")
from mysqlpython import Mysqlpython from hashlib import sha1 # 接受用户输入的用户名和密码 uname = input("请输入用户名:") pwd = input("请输入密码:") # 把用户输入的密码sha1加密 s1 = sha1() # 创建一个对象 s1.update(pwd.encode("utf8")) pwd2 = s1.hexdigest() # 返回16进制加密结果 # 两个密码做对比 sqlh = Mysqlpython("db5") sel = 'select password from user where username=%s' r = sqlh.all(sel, [uname]) if len(r) == 0: print("用户不存在") elif pwd2 == r[0][0]: print("登录成功") else: print("密码错误")
'''测试mysqlpython''' from mysqlpython import Mysqlpython sqlh = Mysqlpython('country') # sql_update = "update sheng set s_name='福建省'\ # where s_name='黑龙江省'" sql_select = "select * from sheng where id = %s;" # sqlh.sql(sql_update) select_info = sqlh.all(sql_select, [1]) print(select_info)
from mysqlpython import Mysqlpython db = Mysqlpython("test") sele = "select keywords,count(keywords) from keywords group by keywords order by count(keywords) desc " s = db.all(sele) print(s)