使用ldap模块的python2脚本中的语法错误

时间:2016-10-16 19:54:52

标签: python python-2.7

学习python(因其ldap模块而被选中)以获得一个被我抛弃的新脚本。我尝试使用ldif时遇到sytntax错误。我在我试图分配的attrs上收到语法错误,直到我将其进一步向上移动到搜索字段附近。我不确定为什么我会收到语法错误:

  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

代码目前看起来如下(包括我尝试使用它们时我曾尝试过的所有语法错误)。让它登录并搜索用户很容易,但修改用户是我遇到困难的地方。当前的代码是未注释的,来自我在网上找到的一个例子。

#!/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)

任何指向导致错误的方向都将非常感激。感谢

2 个答案:

答案 0 :(得分:0)

如果没有尝试,这是一个语法错误。因为除了之外还有很多未缩减的代码,Python并没有将其视为尝试的一部分。确保try和except之间的所有内容都是缩进的。

答案 1 :(得分:0)

到达此行时,您尚未结束try阻止

ldif = modlist.addModlist(attrs)

因为随附的except如下。但是,您减少了缩进级别,这导致语法错误,因为同一块中的内容应该具有相同的缩进。