-2

I'm trying to connect to a server using LDAP. I've gotten the script to work on a test server that didn't use a specific port number. When trying to connect to our Dev-System, which uses a specific port I receive the following error:

File "site-packages\ldap3\core\server.py", line 117, in__init__
ldap3.core.exceptions.LDAPInvalidPortError: port must be an integer
[7836] Failed to execute script ldap_query

In the past we used python-ldap which didn't have an issue with a specified port in the ldap.initialize('LDAP://cd-dir...net/(Port)') command. The piece of code that generates the error can be seen below.

def ldap_connect(address, dn, password)
    server = Server(address)
    try:
        conn = Connection(server, dn, password, auto_bind = True)
        print('Authentication Successful')
        print(conn.extend.standard.who_am_i())
    except: LDAPBindError as err:
        print(LDAPBindError)

ldap_connect('LDAP://cd-dir-cd-test....net:port/dc=cd...dc=com', 'user', 'password') 

To solve the issue I tried taking the port number out of the address and instead put it in the following way:

server = Server(address, port = XXX)

That solved the "port must be an integer" error. However, that didn't solve the problem. The new error that I'm receiving is:

File "site-packages\ldap3\core\connection.py", line 325, in__init__
File "site-packages\ldap3\core\connection.py", line 340, in do_auto_bind
File "site-packages\ldap3\strategy\sync.py", line 56, in open
File "site-packages\ldap3\strategy\base.py", line 151, in open
ldap3.core.exceptions.LDAPSocketOpenError: invalid server address
[5976] Failed to execute script ldap_query

How can I solve this issue? Is there another way to set the port that I don't know of?

Best wishes,

  • It did solve the problem. You got past that problem to a new problem. The new problem is now in whatever the content of `address` is. – user207421 Aug 13 '19 at 12:13
  • I'm not sure if that actually solved the problem. The address is the same string that is used in many other applications so it shouldn't be causing any issues. If I remove the port from the address string I get error 2. If I leave it in, even with port=XXX I get error 1. – Christopher Zerbe Aug 13 '19 at 12:22
  • That is not syntactically valid Python code. – larsks Aug 13 '19 at 13:07

1 Answers1

1

You're passing an ldap:// URI to ldap_connect, but it looks like the ldap3.Server class expects an hostname or address. That is, you're currently trying to do this:

server = Server('ldap://cd-dir-cd-test.example.net:port')

When what you need is:

server = Server('cd-dir-cd-test.example.net', port=port)

And of course port must be an integer, not a string. You can use the ldap3.utils.uri.parse_uri method to extract the information you want from an ldap URI:

from ldap3 import Server, Connection
from ldap3.utils.uri import parse_uri


def ldap_connect(uri, dn, password):
    parsed = parse_uri(uri)
    server = Server(parsed['host'], use_ssl=parsed['ssl'], port=parsed['port'])
    conn = Connection(server, dn, password, auto_bind = True)
    print('Authentication Successful')
    print(conn.extend.standard.who_am_i())

    return conn

conn = ldap_connect('LDAP://cd-dir-cd-test....net:port/dc=cd...dc=com',
                    'user', 'password') 
larsks
  • 277,717
  • 41
  • 399
  • 399