Python implementation of OpenSSL locking callback

Python version of #352 that fixes #330 as suggested by @alex