Syntax error in python2 script using ldap module
<p>Learning python (was chosen for its ldap module) for a new script that has been tossed my way. I'm getting a sytntax error when I try using a ldif. I was getting Syntax errors on the attrs I was trying to assign until I moved it further up the script to near the search fields. I'm not exactly sure why I am getting the syntax error:</p>

<pre><code>  File "UserGroupModify.py", line 66
    attrs = {}
        ^
SyntaxError: invalid syntax

~/Scripts/Termination-Script$ python2 UserGroupModify.py
  File "UserGroupModify.py", line 69
    ldif = modlist.addModlist(attrs)
       ^
SyntaxError: invalid syntax
</code></pre>

<p>The code currently looks like the following (including previous things I had tried all with syntax errors of their own when I tried to use them). Getting it to log in and search for the user was easy enough, but modifying the user is where I am having a hard time. The current code is uncommented and is from an example I found online.</p>

<pre><code>#!/usr/bin/env python2

import ldap
import getpass
import ldap.modlist as modlist

## first you must open a connection to the server
try:

#Ignore self signed certs
    ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)

    username = raw_input("LDAP Login: ")
    passwd = getpass.getpass()
    userlook = raw_input("User to lookup: ")    

    l = ldap.initialize("ldaps://ldap.example.com:636/")

    # Bind/authenticate with a user with apropriate rights to add objects
    l.simple_bind_s("uid="+username+",ou=people,dc=example,dc=com", ""+passwd+"")
except ldap.LDAPError, e:
    print(e)


# The dn of our existing entry/object
dn = "ou=People,dc=example,dc=com"

searchScope = ldap.SCOPE_SUBTREE
searchAttribute = ["uid"]
#retrieveAttributes = ["ou=Group"]
retrieveAttributes = ["ou"]
#searchFilter = "uid=*"
searchFilter = "(uid="+userlook+")"
#mod_attrs = [(ldap.MOD_REPLACE, 'ou', 'former-people' )]
attrs = {}
attrs['member'] = ['uid="+userlook+",ou=former-people,dc=example,dc=com']

try:
    #ldap_result_id = l.search(dn, searchScope, searchFilter, retrieveAttributes)
    ldap_result_id = l.search(dn, searchScope, searchFilter, retrieveAttributes)
    while 1:
        result_type, result_data = l.result(ldap_result_id, 0)
        if (result_data == []):
            break
        else:
            ## here you don't have to append to a list
            ## you could do whatever you want with the individual entry
            ## The appending to list is just for illustration. 
            if result_type == ldap.RES_SEARCH_ENTRY:
                print(result_data)
# Some place-holders for old and new values
#old={'Group':'l.result(ldap_result_id, 0)'}
#new={'Group':'uid="+userlook+",ou=former-people,dc=example,dc=com'}
#newsetting = {'description':'I could easily forgive his pride, if he had not mortified mine.'}
#print(old)
#print(new)

# Convert place-holders for modify-operation using modlist-module
#ldif = modlist.modifyModlist(old,new)

# Do the actual modification 
#l.modify_s(dn,ldif)


#l.modify_s('uid="+userlook+,ou=People,dc=example,dc=com', mod_attrs)
#l.modify_s('uid="+userlook+",ou=People', mod_attrs)

#moved up due to SyntaxError
#attrs = {}
#attrs['member'] = ['uid="+userlook+",ou=former-people,dc=example,dc=com']

# Convert our dict to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(attrs)

# Do the actual synchronous add-operation to the ldapserver
l.add_s(dn,ldif)

# Its nice to the server to disconnect and free resources when done
l.unbind_s()

except ldap.LDAPError, e:
    print(e)
</code></pre>

<p>Any direction pointing on what's causing the error would be greatly appreciated. Thanks</p>

<p>It's a syntax error to have try without except. Because there's a whole lot of unindented code before the except, Python doesn't see it as part of the try. Make sure everything between try and except is indented.</p>

python python-2.7