I am attempting to make an API call to an application on a local machine. It works perfectly when I set verify=False
in my test environment, however my understanding is that this is not best practice on a production server. When I set verify=True
I get an error saying certificate verify failed: self-signed certificate
From my research, I believe I should be attempting to get the certificate as a pem file and point the verify
variable to that location. I am attempting to do that with the following code:
cert = ssl.get_server_certificate(('hostname', portNo))
with open('avam.pem','w') as file:
file.write(cert)
nodes = requests.get('https://hostname:port/api/nodes', verify='avam.pem')
This throws the error:
SSLError: HTTPSConnectionPool(host=’hostname’, port={portNo}): Max retries exceeded with url: /api/nodes/ (Caused by SSLError(SSLCertVerificationError(1, “[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for ‘hostname’. (_ssl.c:1000)”)))
I am able to see the .pem file, and it appears to be a proper certificate (----BEGIN CERTIFICATE----
+ a long string + -----END CERTIFICATE-----
Are there any suggestions as to what else I could be missing here, or what routes I might be able to take? I have seen some similar issues here on SO, but my understanding of SSL is pretty basic, so I am looking for some advice specific to my situation.
6