python单用户登录_python 简单的用户登陆

#!/usr/bin/env python

#coding:utf-8

import hashlib

from datetime import datetime

ul = {}

def newuser():

a = []

prompt = "Please enter username again:"

while True:

name = raw_input(prompt).lower()

if not name.isalnum() and ‘‘ in name :

print(‘name format error‘)

continue

else :

if ul.has_key(name):

prompt = "username already exists"

continue

else:

break

passwd = raw_input(‘enter password:‘)

m=hashlib.md5()

m.update(passwd)

a.append(m.hexdigest())

a.append(datetime.now())

ul[name]=a

print ‘new user is %s ‘           ‘regiter time is %s‘ %(name,ul[name][1])

def olduser():

name= raw_input(‘Please enter username again:‘).lower()

passwd = raw_input(‘Please enter your password:‘)

m=hashlib.md5()

m.update(passwd)

pwd = ul.get(name)

if pwd[0]==m.hexdigest():

newtime = datetime.now()

if (newtime-ul[name][1]).days == 0 and (newtime-ul[name][1]).seconds 

print ‘you already logged in at %s: ‘ %(ul[name][1])

else:

pwd[1]=newtime

print ‘welcome back %s, login time is %s‘ %(name,passwd[1])

else:

print ‘login incorrect‘

def removeuser():

print ul

name=raw_input(‘input a user name to remove: ‘).lower()

if name in ul:

ul.pop(name)

print ‘remove successful‘

else:

print ‘this uesr not exist‘

def login():

while True:

name=raw_input(‘Please enter username:‘).lower()

if not name.isalnum() and ‘‘ in name:

print ‘name format error‘

continue

else:

if not ul.has_key(name):

print ‘user name is not in userlist‘

answer=raw_input(‘register a new user? (y/n):‘).lower()

if ‘y‘==answer:

newuser()

break

elif ‘n‘==answer:

break

else:

print ‘user name is already in db‘

olduser()

break

def showmenu():

prompt = """

(U)ser login

(R)emove user

(Q)uit

Enter choice: """

done = False

while not done:

chosen = False

while not chosen:

try:

choice = raw_input(prompt).strip()[0].lower()

except (EOFError,KeyboardInterrupt):

choice= ‘q‘

print"\nYou picked [%s]" %choice

if choice not in ‘urq‘:

print("invalid option,try again")

else :

chosen = True

if choice == ‘q‘:done = True

if choice == ‘u‘:login()

if choice == ‘r‘:removeuser()

if __name__ == ‘__main__‘:

showmenu()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值