昨天浪费了一个晚上在改密码,今天那些密码全都忘了,郁闷。所以,我想要这样的一个东西:将我所有的账号和对应的密码以二进制的格式储存在硬盘上。运行之后大概会是这样的一个东西:
1)启动。选项:a.查看所有账号和密码;b.查找账号和密码;c.增加账号和密码;d.修改账号和密码;e.删除账号和密码。输入的选项忽略大小写。
2)我想以一个字典存储数据,键 key_site 为所要保存的站点名,或软件名,其值 value 为另一个字典,该字典的 key_account 为账号,value_passwd 为密码。查看所有的账号和密码,那按某种格式打印字典就可以了。查找、修改以及删除都可以很容易地完成。
3)不过存在硬盘上就有一定的风险,以文本文件存储难免会泄露,所以就要将文件存为二进制数据。如何将此字典存为二进制数据,我是没接触过,wb 打开文件等下可以试试,但我估计没用。''' How time flies '''试了下,果然没用。
---------代码如下---------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # -*-coding:UTF-8 -*-
2
3 import json
4
5 __all__ = ['show_all', 'add_uid', 'do_delete',
6 'find_passport', 'modify_passport']
7
8 def show_all():
9 if site_passport_dict:
10 for k, v in site_passport_dict.items():
11 for kv, vv in v.items():
12 print k, kv, vv
13 #print key, ':' , value
14 else:
15 print 'empty...\n'
16
17 def num_of_website(site):
18 #print '4', site
19 num = 1
20 site_list = site_passport_dict.keys()
21 for web_site in site_list:
22 if web_site[:len(site)] == site:
23 num = num + 1
24 else:
25 pass
26 return num
27
28 def add_to_dict(site, uid, passwd):
29 global passport_dict
30 passport_dict = {}
31 passport_dict[uid] = passwd
32 site_passport_dict[site] = passport_dict
33 passport_dict = {}
34 return site_passport_dict
35
36 def add_uid():
37 site = raw_input('Input the website:')
38 uid = raw_input('Input your id:')
39 passwd = raw_input('Input the password:')
40
41 if site in site_passport_dict:
42 u_choice = raw_input('''该网站已有帐号在内,是否继续?
43 (选择继续不会删除原有的账号密码) y(es) or n(o)''')
44 if u_choice.upper() in ('Y', 'YES'):
45 site_num = num_of_website(site)
46 site = site + str(site_num)
47 add_to_dict(site, uid, passwd)
48 else:
49 return
50 else:
51 add_to_dict(site, uid, passwd)
52
53 def find_passport():
54 user_input = raw_input('What site do you want to find?')
55 if user_input in site_passport_dict:
56 user_want_site = []
57 for find_key in site_passport_dict.keys():
58 if find_key[:len(user_input)] == user_input:
59 user_want_site.append(find_key)
60 continue
61 deal_passport(user_want_site)
62 else:
63 print 'Sorry, cannot find [%s].' % user_input
64
65 def deal_passport(user_want_site):
66 for key, value in site_passport_dict.items():
67 if key in user_want_site:
68 print key, ':' , value
69
70 def modify_passport():
71 """ text """
72 while True:
73 mod_site = raw_input('Input sitename you want to modify: ')
74 if mod_site not in site_passport_dict:
75 print 'Cannot not find %s.' % mod_site
76 raw_input('press any key to continue...')
77 else:
78 site_dict = {}
79 i = 0
80 for key, value in site_passport_dict.items():
81 if key[:len(mod_site)] == mod_site:
82 site_dict[i] = key
83 i += 1
84 print 'All site passport: %s ' % site_dict
85 break
86 site_choice = int(raw_input('Input the NO. you want to modify: '))
87 final_mod_site = site_dict[site_choice]
88 mod_uid = raw_input('OK. Input the uid you want to save: ')
89 mod_passwd = raw_input('Input the passwd of this uid: ')
90 passport_dict[mod_uid] = mod_passwd
91 site_passport_dict[final_mod_site] = passport_dict
92 print 'Modify done...'
93 return site_passport_dict
94
95 def do_delete():
96 while True:
97 del_site = raw_input('Input the site you want to delete: ')
98 if del_site not in site_passport_dict:
99 print 'Cannot find: %s' % del_site
100 else:
101 break
102
103 real_del = raw_input('确定删除: [%s] y(es) or n(o)' % del_site)
104 if real_del.upper() in ('Y', 'YES'):
105 print '该网址下共有以下账号:'
106 for k, v in site_passport_dict.items():
107 if del_site in k:
108 print k,
109 del_site = raw_input('\n请从以上列表选择并输入所要删除的网址全称:')
110 del site_passport_dict[del_site]
111 print '删除已完成'
112 else:
113 return
114
115 CMDs = {'s':show_all, 'm':modify_passport,
116 'a':add_uid, 'f':find_passport,
117 'd':do_delete}
118
119 def show_menu():
120 menu = '''\n(s)how_all (m)odify_passport
121 (a)dd_uid (f)ind_passport
122 (q)uick (d)elete
123 Enter choice:'''
124 while True:
125 while True:
126 try:
127 menu_choice = raw_input(menu).strip()[0].lower()
128 except (EOFError, KeyboardInterrupt, IndexError):
129 menu_choice = 'q'
130
131 print 'You picked %s\n' % menu_choice
132 if menu_choice not in 'smafdq':
133 print 'Invalid option, try again'
134 else:
135 break
136 if menu_choice == 'q':
137 #global site_passport_dict
138 #site_passport_dict = {}
139 json_file = json.dumps(site_passport_dict)
140 open('passport.db', 'w').write(json_file)
141 break
142 CMDs[menu_choice]()
143
144 if __name__ == '__main__':
145 try:
146 site_passport_dict = json.loads(open('passport.db').read())
147 passport_dict = {}
148 for k, v in site_passport_dict.items():
149 for kv, vv in v.items():
150 passport_dict[kv] = vv
151 except:
152 print '目录下无账号密码文件,将创建:passport.db'
153 site_passport_dict = {}
154 passport_dict = {}
155
156 show_menu()
---------运行效果---------
这样其实也是明文存储,如果你怕被某杀软上传至其服务器,那还是通过各有效地加密保存方法吧。
下次做成一个数据库存储的程序==
---------全文结束---------