Problem with ssl in Python 2.5
I'm beginning to think about the posibility that installing Python from MacPorts in Leopard, and forgetting about the default version that comes with the operating system is not a good idea.
Seems like the guy behind the python25 port decided to split some of the builtin modules into separate ports (I'm not going to explain my oppinion about splitting things into small packages here :D) and today I found that SSL socket support is one of those separate packages.
I was working on a module to add support for sending data by mail from a webapp I've created on top of Django. I followed all the steps in the official docs, so I added some lines into my project settings.py:
# SMTP Settings
DEFAULT_FROM_EMAIL = 'info@gospamyourself.net'
EMAIL_HOST='mail.gospamyourself.net'
EMAIL_PORT=25
EMAIL_HOST_USER='info@gospamyourself.net'
EMAIL_HOST_PASSWORD='thisismypassthisismypass####'
EMAIL_USE_TLS=True
As I've set EMAIL_USE_TLS to True, the django internals code that will send the email for me will try to use a secure TLS link to send the email to the server.
I saved the settings and tried my code, just to notice an ugly message:
'module' object has no attribute 'ssl'
more precisely:
Traceback:
File "/opt/local/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
82. response = callback(request, *callback_args, **callback_kwargs)
File "/opt/local/lib/python2.5/site-packages/django/contrib/auth/decorators.py" in __call__
67. return self.view_func(request, *args, **kwargs)
File "/Users/wu/codigo23/devel_python/Beyle/FMJJ/../FMJJ/beyle/views.py" in enviar_por_mail
2009. email_obj.send()
File "/opt/local/lib/python2.5/site-packages/django/core/mail.py" in send
259. return self.get_connection(fail_silently).send_messages([self])
File "/opt/local/lib/python2.5/site-packages/django/core/mail.py" in send_messages
162. new_conn_created = self.open()
File "/opt/local/lib/python2.5/site-packages/django/core/mail.py" in open
130. self.connection.starttls()
File "/opt/local/lib/python2.5/smtplib.py" in starttls
605. sslobj = socket.ssl(self.sock, keyfile, certfile)
Exception Type: AttributeError at /beyle/obra/2873/mail/
Exception Value: 'module' object has no attribute 'ssl'
mmm... mumble, mumble (as old mickey mouse should say)
What happened here? it seems like Python wasn't able to connect using the secure link... Let's test if there is any problem with that...
Python comes by default with a set of testing scripts you can use to (know what?) test some functionalities. one of those test scripts is testsocketssl.py, which seemed perfect for do what I needed. But... wait a second... seems like that file doesn't exist in my Python installation:
snowball:~ wu$ ls -l /opt/local/lib/python2.5/test/testsocketssl.py ls: /opt/local/lib/python2.5/test/testsocketssl.py: No such file or directory snowball:~ wu$
Only then, I began to think about the posibility of a separated port, so I took a look over the MacPorts database, just to find...
snowball:~ wu$ port search py|grep ssl py-openssl python/py-openssl 0.6 python wrapper around the OpenSSL library py-pow python/py-pow 0.7 Python OpenSSL Wrappers is an interface to the openssl library py25-socket-ssl python/py25-socket-ssl 2.5.2 Python secure sockets via OpenSSL py30-socket-ssl python/py30-socket-ssl 3.0a3 Python secure sockets via OpenSSL snowball:~ wu$
There it was! a py25-socket-ssl port. I proceeded installing it:
sudo port -v install py25-socket-ssl
And everything works fine since then. Take care, as some other interesting/important modules (like the new native SQLite support) are in separate ports too. You can get a list of python25 related ports with:
port search py25
And remember, once you find an unusual error, just think about a separate port.