'"SSL.CERTIFICATE_VERIFY_FAILED: Self signed certificate in certificate chain" in python with PROTOCOL_TLS_CLIENT

This was working fine when I was using PROTOCOL_TLS, but since it's deprecated, I'm trying to switch to PROTOCOL_TLS_CLIENT. The code goes as follows:

sslSettings= ssl.SSLContext();
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)

context.load_verify_locations("certificate.pem");
context.load_cert_chain(certfile="certificate.pem", keyfile="keystore.p12")

# Create a connection to submit HTTP requests
connection = http.client.HTTPSConnection(host, port=443, context=context)

# Use connection to submit a HTTP POST request
connection.request(method=method, url=url, headers=headers, body=json.dumps(body))

Certificate.pem is a file given by the server I'm making the request to, and keystore.p12 is the private key I extracted from certificate.pem. I've also tried creating a .crt file from certificate.pem and use that for load_verifiy_locations but the error maintained.

Any help is appreciated, thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source