我在Python中有一个作业脚本,它调用命令来更新AD用户。当我在本地机器上运行此命令时,它工作正常。但是在我们的远程服务器中,使用Linux和Apache,相同的脚本不起作用并给我一个错误:“未知命令:'ldap_sync_users'...我尝试使用其他常用命令,如迁移(仅用于测试)和它给出同样的问题...
这是我正在尝试运行并在我的本地运行正常的命令:
# https://github.com/etianen/django-python3-ldap
management.call_command('ldap_sync_users')
我正在使用Python 3.6,Django 2.0.4。在localserver我使用virtualenv和远程服务器Apache 2.我做错了什么?有人可以帮帮我吗?
view.py
...
from apps.iumob.utilities import sync_hr_with_ldap
def cron_job_to_sync(request):
print(request.GET.get('token'))
if request.GET.get('token') == "mytoken":
if sync_hr_with_ldap() is True:
return HttpResponse("Records synchronization finished.")
else:
return HttpResponse("Records synchronization finished with errors.")
else:
return HttpResponse("Access forbidden.")
utilities.py:
from django.core import management
def sync_hr_with_ldap():
"""
This method will be called to synchronize AD users and workers.
If some worker that have a user and he was fired, it will check all users and, if it is still active,
it will save in database this record. Every time that the user is deactivate from AD,
this record will also be updated.
"""
# This method call a library that call LDAP and check all users, inactivating if necessary
# https://github.com/etianen/django-python3-ldap
management.call_command('ldap_sync_users')`
回溯
Environment:
Request Method: GET
Request URL: myurl.../pt-br/iumob/cron-job-sync?token=sometoken
Django Version: 2.0.3
Python Version: 3.5.2
Installed Applications:
['apps.iumob.apps.IumobConfig',
'apps.hrmob.apps.HrmobConfig',
'apps.cimob.apps.CimobConfig',
'apps.users.apps.UsersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_python3_ldap',
'rest_framework',
'rest_framework.authtoken']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py" in call_command
102. app_name = get_commands()[command_name]
During handling of the above exception ('ldap_sync_users'), another exception occurred:
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/marcomob/apps/iumob/views.py" in cron_job_to_sync
43. if sync_hr_with_ldap() is True:
File "/var/www/marcomob/apps/iumob/utilities.py" in sync_hr_with_ldap
19. management.call_command('ldap_sync_users')
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py" in call_command
104. raise CommandError("Unknown command: %r" % command_name)
Exception Type: CommandError at /pt-br/iumob/cron-job-sync
Exception Value: Unknown command: 'ldap_sync_users'