#PYTHON DOWNLOAD FILE FROM URL AND SAVE CODE#
I am hoping this code will help those that are new to Python and SSL like me. I have learned different certificate formats and security protocol that can be used as well. I learned the difference between Python 2.7.15 and 3.7.2 while writing this code. To use either of these modules, importing it and perform a call.
#PYTHON DOWNLOAD FILE FROM URL AND SAVE INSTALL#
The two formers are external modules you have to install before making use of them into your source code while the latter is a Python built-in module. I have learned the feature through articles and Q & A along with OpenSSL and Python documentation.Ĭopy Code def get_certificate(host, port, cert_file_pathname):Ĭontext = SSL.Context(SSL.TLSv1_2_METHOD) As we said, there are some methods of downloading files from the internet by using one of these modules: requests, wget or urllib. I would like to share this code with everyone as I realized that there aren't many article about this topic. You can modify the code to serve your purpose as needed. Notice that I manipulate cert_file_pathname and append the index of the certificate to certfile in the code so that I can store all the downloaded certificate with the same prefix. But you can retrieve other fields as needed. In the code snippet, I iterate through the certificate list and retrieve the certificate's CN, then print out the CN string. The code below is a sample Python snippet that will connect to host (e.g., any at specified port (e.g., 443), download certificate chain from host, and store the certificates on the specified cert_file_pathname (e.g., c:\testfolder\certfile). My purpose is to retrieve certificate chain and store the certificates on my local drive which can be further used in other modules. Not much information was found on this topic. I searched through lots of public articles and lots of Q & A on this topic.
While doing POC, I stumbled upon the versioning conflict of Python 2.7.x and Python 3.7.x. I wrote this article to pass on my knowledge to other developers who might have stumbled upon a different version of Python (Python 2.7.x VS Python 3.7.x) when using OpenSSL to download, view, and save certificates.