@@ -258,7 +258,7 @@ class _TLSMessageType:
258258 from _ssl import enum_certificates , enum_crls
259259
260260from socket import socket , SOCK_STREAM , create_connection
261- from socket import SOL_SOCKET , SO_TYPE
261+ from socket import SOL_SOCKET , SO_TYPE , _GLOBAL_DEFAULT_TIMEOUT
262262import socket as _socket
263263import base64 # for DER-to-PEM translation
264264import errno
@@ -1500,11 +1500,14 @@ def PEM_cert_to_DER_cert(pem_cert_string):
15001500 d = pem_cert_string .strip ()[len (PEM_HEADER ):- len (PEM_FOOTER )]
15011501 return base64 .decodebytes (d .encode ('ASCII' , 'strict' ))
15021502
1503- def get_server_certificate (addr , ssl_version = PROTOCOL_TLS_CLIENT , ca_certs = None ):
1503+ def get_server_certificate (addr , ssl_version = PROTOCOL_TLS_CLIENT ,
1504+ ca_certs = None , timeout = _GLOBAL_DEFAULT_TIMEOUT ):
15041505 """Retrieve the certificate from the server at the specified address,
15051506 and return it as a PEM-encoded string.
15061507 If 'ca_certs' is specified, validate the server cert against it.
1507- If 'ssl_version' is specified, use it in the connection attempt."""
1508+ If 'ssl_version' is specified, use it in the connection attempt.
1509+ If 'timeout' is specified, use it in the connection attempt.
1510+ """
15081511
15091512 host , port = addr
15101513 if ca_certs is not None :
@@ -1514,7 +1517,7 @@ def get_server_certificate(addr, ssl_version=PROTOCOL_TLS_CLIENT, ca_certs=None)
15141517 context = _create_stdlib_context (ssl_version ,
15151518 cert_reqs = cert_reqs ,
15161519 cafile = ca_certs )
1517- with create_connection (addr ) as sock :
1520+ with create_connection (addr , timeout = timeout ) as sock :
15181521 with context .wrap_socket (sock , server_hostname = host ) as sslsock :
15191522 dercert = sslsock .getpeercert (True )
15201523 return DER_cert_to_PEM_cert (dercert )
0 commit comments