LDAP连接报错LDAPSessionTerminatedByServerError:会话被服务器终止求助
Troubleshooting LDAP Session Terminated by Server Error
Let’s work through your LDAP connection issue together—here’s what I notice and how to fix it:
1. Immediate Red Flags in Your Code & Output
First, let’s spot the obvious mismatches and syntax errors that are likely triggering the server to terminate your session:
- Password Syntax Issue: Your code uses
password=user-pass—in Python, variable names can’t include hyphens. If this is a hardcoded password, it needs quotation marks (e.g.,password="your-actual-password"). If it’s a variable, rename it to something likeuser_passwordto follow Python’s naming rules. A malformed password will definitely get your connection rejected. - Mismatched User DN: Your code specifies
user='uid=admin,ou=people,dc=example,dc=org', but the connection output shows the server is seeinguid=admin,ou=people,dc=hadoop,dc=apache,dc=org. This means either your code has the wrong DN, or your LDAP server’s admin account lives under thedc=hadoop,dc=apache,dc=orgtree. Using an incorrect DN will fail authentication and trigger a session termination.
2. Step-by-Step Fixes & Troubleshooting
Here’s how to resolve this:
- Fix the Password First: Correct the password line to use valid Python syntax. For example:
# If using a hardcoded password: password="your-admin-password" # If using a variable: user_password = "your-admin-password" password=user_password - Verify the Admin DN: Confirm the correct DN for your LDAP admin account using a command-line tool like
ldapsearch:
Replaceldapsearch -x -b dc=example,dc=org '(uid=admin)'dc=example,dc=orgwith your actual LDAP base DN. This will return the full, correct DN of the admin user—use that exact string in your code. - Check Server Connectivity: Ensure the LDAP server is running on
localhost:33389. Test the port withncortelnet:
If this fails, your LDAP service isn’t running or the port is blocked.nc -zv localhost 33389 - Enable Detailed Debugging: Modify your connection to raise exceptions and auto-bind immediately—this will give you clearer, specific error messages:
Withfrom ldap3 import Server, Connection, ALL, SIMPLE server = Server(host='localhost', port=33389, use_ssl=False, get_info=ALL) # Use the correct DN and fixed password here conn = Connection(server, user='uid=admin,ou=people,dc=example,dc=org', password='your-admin-password', raise_exceptions=True, authentication=SIMPLE, auto_bind=True) print(server.info) print(conn)raise_exceptions=True, you’ll get precise details about why the server is terminating the session (e.g., invalid credentials, missing permissions).
3. Additional Checks
- Confirm the admin account has permission to connect from your host—some LDAP servers restrict connections to specific IP addresses.
- Check the LDAP server’s logs (usually in
/var/log/ldap/or a similar directory) for more context on why the session was terminated.
内容的提问来源于stack exchange,提问作者user5431918
相关产品推荐
相关产品推荐

