Method Name:
getpeercert
Method Signature:
getpeercert(binary_form = False)
Parameters:
binary_form – Boolean value that determines the format in which the certificate is returned. If True is passed, the certificate is returned as a Python dictionary. If False is passed, the certificate is returned in DER(Distinguished Encoding Rules) binary format as a Python string.
Return Value:
- If a certificate is available at the other end of the communication, a Python dictionary containing certificate information or a Python string in DER format is returned.
- The format of the returned certificate is based on the value passed to the binary_form parameter.
Exceptions:
- ValueError, if the call precedes the SSL handshake.
Overview:
- The method getpeercert() retrieves the digital certificate available if any, from the other end of the communication.
- The retrieved certificate is in the form of a Python dictionary if the parameter binary_form is False. Otherwise, the certificate is in the DER binary format. A certificate in DER format can be converted to human readable PEM format using the function der_cert_to_pem_cert(). Similarly, a certificate in PEM format can be converted to DER format using the function pem_cert_to_der_cert().
Example:
# Example Python program that uses a client socket to # certificate # Create an SSL context # Check for OS X platform # Load the CA certificates used for validating the peer's certificate # Create a client socket # Get an instance of SSLSocket # Get the SSL certificate of the peer print("X.509 certificate obtained from the server:") |
Output:
X.509 certificate obtained from the server: |