gyn

Win32下的Perl,无用的select,停滞的Tk,结束吧....

给上一篇中的ftp服务增加了用户添加和删除的程序

最终放弃GUI 了,太麻烦。命令行好搞啊,呵呵。
 1import os, sys
 2import md5
 3from pysqlite2 import dbapi2 as sqlite
 4
 5def user_insert(username, password, homedir, perm, msg_login, msg_quit):
 6    if not username: username = 'anonymous'
 7    if not os.path.exists(homedir): homedir = r'D:\None'
 8    if not perm in ('''r;''r;w'): perm = 'r;'
 9    if not msg_login: msg_login = 'Login successful.'
10    if not msg_quit: msg_quit = 'Goodbye.'
11    dbpath = '%s\\users.db' % os.getcwd()
12    if not os.path.isfile(dbpath):
13        con = sqlite.connect(dbpath, isolation_level = None)
14        cur = con.cursor()
15        cur.execute("CREATE TABLE users(username test primary key, password text, homedir text, perm text, msg_login text, mag_quit text)")
16        cur.close()
17        con.close()
18    con = sqlite.connect(dbpath, isolation_level = None)
19    cur = con.cursor()
20    cur.execute("insert into users values(?, ?, ?, ?, ?, ?)"
21                (username, password, homedir, perm, msg_login, msg_quit))
22    cur.close()
23    con.close()
24
25try:
26    from msvcrt import getch
27except ImportError:
28    def getch():
29        import tty, termios
30        fd = sts.stdin.fileno()
31        old.settings = termios.tcgetattr(fd)
32        try:
33            tty.setraw(fd)
34            ch = sys.stdin.read(1)
35        finally:
36            termios.tcsetattr(fd, tremios.TCSADRAIN, old.settings)
37        return ch
38
39def asterisk_raw_input(label):
40    sys.stdout.write(label)
41    strAttr = []
42    while True:
43        tmp = getch()
44        if tmp != '\r':
45            strAttr.append(tmp)
46            sys.stdout.write('*')
47        else
48            sys.stdout.write('\n')
49            break
50    return ''.join(strAttr)
51    
52    
53user_info_list = []
54print "-*- pyftpdlib user information. -*-\n-*- Separated by ':' -*-\n"
55for eachLabel in ("Username""Password""Homedir""Perm""Msg_login""Msg_quit"):
56    if eachLabel == 'Password':
57        user_info = md5.new(asterisk_raw_input(eachLabel + ':  ')).hexdigest()
58    else:
59        user_info = raw_input(eachLabel + ':  ')
60    user_info_list.append(user_info)
61user_insert(*user_info_list)
62print "-*- Programme Quit. -*-"

user_add.py
 1import os
 2from pysqlite2 import dbapi2 as sqlite
 3
 4user_info = {}
 5dbpath = '%s\\users.db' % os.getcwd()
 6if not os.path.isfile(dbpath):
 7    print '-*- No user to delete. -*-'
 8else:
 9    con = sqlite.connect(dbpath, isolation_level = None)
10    cur = con.cursor()
11    cur.execute('select username from users')
12    result = cur.fetchall()
13    if not result: 
14        print '-*- No user to delete. -*-'
15    else:
16        result = [name[0] for name in result]
17        for index, name in zip(range(1, len(result)+1), result):
18            print '<%d> %s' % (index, name)
19            user_info[index] = name
20        print ''
21        user_index = int(raw_input("Enter the index of user to delete: "))
22        while not user_index in range(0, len(result)+1):
23            user_index = raw_input("Wrong index, re_enter: ")
24        if user_index:
25            cur.execute("delete from users where username = '%s'" % user_info[user_index])
26            print "-*- '%s' is deleted. -*-" % user_info[user_index]
27    cur.close()
28    con.close()
29    print "-*- Programme Quit. -*-"
user_del.py

posted on 2007-11-11 15:37 gyn_tadao 阅读(354) 评论(0)  编辑 收藏 引用 所属分类: Python

只有注册用户登录后才能发表评论。
<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(15)

随笔分类(126)

随笔档案(108)

相册

搜索

最新评论

阅读排行榜

评论排行榜