Python: Keychain.py access to the Mac OSX keychain | Comments (4)
Posted in Code on 5th February 2008, 11:53 pm by Stuart
The code in the project has changed somewhat since this post – see the project for more info

I’ve been busy hacking away on a simple little class that makes it really easy to get access to the keychain on mac osx. I’d seen already a way to acces the keychain in this post by Mark Rowe however I wanted to avoid the dependency on objc.
To solve the problem I wrapped the security command which provides fairly comprehensive access to the keychain.
Where this comes in handy is any time that you want to create a Python CLI app that could do with keychain access you can use keychain.py to access credentials stored in the keychain. In addition keychain.py allows keychain settings to be altered and read and you can also create new keychains and keychain items.
Here’s a couple of simple examples of how you can use keychain.py in a script.
Instantiation
>>> import keychain
>>> k=keychain.Keychain();
Create a keychain
>>> k.createkeychain('TopSecret','Shhhssh')
(True, 'Keychain created successfully')
List keychains
>>> k.listkeychains()
{'System': '/Library/Keychains/System.keychain', 'TopSecret': '/Users/muffinman/Library/Keychains/TopSecret.keychain', 'login': '/Users/muffinman/Library/Keychains/login.keychain'}
Set and get a keychain item
>>> k.setgenericpassword('TopSecret', 'muffin','test')
(True, 'Password added to TopSecret.keychain successfully')
>>> k.getgenericpassword('TopSecret', 'muffin')
{'account': 'muffin', 'password': 'test'}
The code is on launchpad.net so if you would like to try it out you can simply checkout the code or download it
As always all feedback and suggestions welcome.

[...] bookmarks tagged osx Python: Keychain.py access to the Mac OSX keychain saved by 30 others Hanna467 bookmarked on 02/07/08 | [...]
Ok, that’s a really cool icon !
Very nice. Thank you!
Thanks a lot!