Python Friday #204: Using Feedparser With Self-Signed Certificates

Feedparser is a great Python library to read RSS and ATOM feeds that I covered in Python Friday #102. When I tried to read a local RSS feed with Feedparser, I ran into a problem with my self-signed SSL certificate. Let us see how we can tell Feedparser that our certificate is trustworthy.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

The problem

I created my feed in a C# application that runs over HTTPS using the developer certificate from Visual Studio. When I try to read that RSS feed with Feedparser, I got this error message:

SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1002)’)

 

The solution

This error is common enough that I found a solution without much effort. All we need to do is to add the tree lines (2-5) before we instantiate Feedparser:

This tells the SSL module to use an unverified context as the default HTTPS context in which SSL does not verify the certificates. This will not only accept my self-signed certificate, but any certificate.

Therefore, be careful with using this solution. It may be too much for your scenario.

 

Next

This little hack solved my problem, but it introduced more questions. How does this work? And what is going on inside Python that allows me to do that? The name for this practice is monkey patching and we will explore it in more details next week.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.