title: Connecting to AD with LDAP in Python - Error 'unable to open socket', [(LDAPSocketOpenError("('socket ssl wrapping error: [SSL] PEM lib (_ssl.c:3895)'
body: <p>When connecting to MS-AD to perform a password reset using the following Python script with LDAP3 library and SSL protocol, I am facing a problem connecting.</p>
  <pre><code>import ldap3 as ldap
  import ssl
  
  # &lt;-------------------------------Adicionar Certificado a coneção-----------------------------------&gt;
  certificado = &quot;&lt;certificado&gt;.cer&quot;
  context = ssl.create_default_context()
  
  #tls = Tls(validate=ssl.CERT_NONE)
  tls = ldap.Tls(validate=ssl.CERT_OPTIONAL , version=ssl.PROTOCOL_TLS, local_certificate_file=certificado)
  
  #------------------------------&lt; Variavel AE &gt;---------------#
  dominio = ldap.Server('TESTE.LOCAL', 636, tls=tls, get_info=ldap.ALL, use_ssl=True)
  user='&lt;usuario&gt;'
  pwd='&lt;pass&gt;'
  usuarioDN = '&lt;user for reset&gt;'
  newPass = '&lt;new pass&gt;'
  
  # &lt;--------------------------------Conexão com AD LDAP com autenticação-----------------------------------&gt;
  connect = ldap.Connection(server=dominio, 
                          user=user, 
                          password=pwd, 
                          read_only = True, 
                          auto_bind=True,
                          version=3,
                          authentication='SIMPLE')
  
  
  print(connect)
  
  # &lt;------------------------------Realizar a alteração da Senha-------------------------------------&gt;
  connect.extend.microsoft.modify_password(usuarioDN, newPass)
  print(connect.result)
  
  # &lt;-------------------------------Exigir troca de senha no próximo Logon ---------------------------------------------&gt;
  connect.modify(usuarioDN,{&quot;pwdLastSet&quot;:(ldap.MODIFY_REPLACE, 0)})
  print(connect.result)
  </code></pre>
  <p>As it is a secure server, a certificate is required to authenticate the connection, where I have already performed the tests both by installing the certificate on the server where the script is running and also informing the certificate in the code.</p>
  <p>Still it always returns the error as below.</p>
  <pre><code>Exception has occurred: LDAPSocketOpenError
  ('unable to open socket', [(LDAPSocketOpenError(&quot;('socket ssl wrapping error: [SSL] PEM lib (_ssl.c:3895)',)&quot;), ('***.***.***.***', 636)), (LDAPSocketOpenError(&quot;('socket ssl wrapping error: [SSL] PEM lib (_ssl.c:3895)',)&quot;), ('***.***.***.***', 636)), (LDAPSocketOpenError(&quot;('socket ssl wrapping error: [SSL] PEM lib (_ssl.c:3895)',)&quot;), ('***.***.***.***', 636)), (LDAPSocketOpenError(&quot;('socket ssl wrapping error: [SSL] PEM lib (_ssl.c:3895)',)&quot;), ('***.***.***.***', 636)), (LDAPSocketOpenError(&quot;('socket ssl wrapping error: [SSL] PEM lib (_ssl.c:3895)',)&quot;), ('***.***.***.***', 636))])
    File &quot;C:\Users\sv_eyeshare\Desktop\TPLDAP\ResetSenhaLdap.py&quot;, line 19, in &lt;module&gt;
  </code></pre>
  <p>Has anyone faced this problem that can help me please.</p>