<p>Hello,</p>
<p> </p>
<p>I've always wandered why HTTPSConnection does not validate certificates?</p>
<p>It is fairly simple to use the SSL socket's validation:</p>
<p> </p>
<p>> class HTTPSConnection(HTTPConnection):<br />>    """This class allows communication via SSL.<br />>    It is a copy of the http.client.HTTPSConnection with added certificate validation<br />>    """<br />>    <br />>    default_port = HTTPS_PORT<br />>    <br />>    def __init__(self, host, port=None, key_file=None, cert_file=None, ca_file=None,<br />>                 cert_reqs=ssl.CERT_NONE, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):<br />>        HTTPConnection.__init__(self, host, port, strict, timeout)<br />>        self.key_file    = key_file<br />>        self.cert_file    = cert_file<br />>        self.cert_reqs    = cert_reqs<br />>        self.ca_file    = ca_file<br />>    <br />>    def connect(self):<br />>        "Connect to a host on a given (SSL) port."<br />>        <br />>        sock = socket.create_connection((self.host, self.port), self.timeout)<br />>        <br />>        if self._tunnel_host:<br />>            self.sock = sock<br />>            self._tunnel()<br />>        <br />>        self.sock = ssl.wrap_socket(sock, keyfile=self.key_file, certfile=self.cert_file, cert_reqs=self.cert_reqs, ca_certs=self.ca_file)</p>
<p> </p>
<p>>  conn        = HTTPSConnection(host, cert_file=certfile, cert_reqs=ssl.CERT_REQUIRED, ca_file=cafile)</p>
<p> </p>
<p>IMO it doesn't matter how well is the validation covered in the ssl lib, or the filenames vs. files issues of ssl.wrap_socket() - HTTPSConnection should only provide the means to use what is available in SSL and include a link in docs to explanations of how SSL does what it does, so that people can make their decisions.</p>
<p> </p>
<p>The above code works quite well for me in production, where client nodes connect to apache and nginx servers by HTTPS with certificate based authentication. I'm using self signed CA though and I don't need revocation lists, so I know nothing of whether that part of validation is/could be working.</p>
<p> </p>
<p>Just sharing a simple solution to a simple problem on which I spent more that few hours in reading, hope this helps the next lost soul</p>
<p> </p>
<p> </p>
<p>Best Regards</p>
<p>Velko Ivanov</p>