diff --git base/univention-lib/python/umc.py base/univention-lib/python/umc.py index c9e83aba04..cd022ea230 100644 --- base/univention-lib/python/umc.py +++ base/univention-lib/python/umc.py @@ -41,13 +41,29 @@ connections to remote |UMC| servers import ssl import json import locale -from Cookie import SimpleCookie -from httplib import HTTPSConnection, HTTPException -from univention.config_registry import ConfigRegistry -ucr = ConfigRegistry() -ucr.load() +import six +if six.PY3: + from http.cookies import SimpleCookie + from http.client import HTTPSConnection, HTTPException +else: + from Cookie import SimpleCookie + from httplib import HTTPSConnection, HTTPException + +def _get_ucr(): + from univention.config_registry import ConfigRegistry + ucr = ConfigRegistry() + ucr.load() + return ucr + +def _get_useragent(): + ucr = _get_ucr() + return 'UCS/%s (univention.lib.umc/%s-errata%s)' % (ucr.get('version/version', '0.0'), ucr.get('version/patchlevel', '0'), ucr.get('version/erratalevel', '0')), + +def _get_fqdn(): + ucr = _get_ucr() + return '%s.%s' % (ucr.get('hostname'), ucr.get('domainname')) class _HTTPType(type): """ @@ -340,16 +356,16 @@ class Client(object): ConnectionType = HTTPSConnection - def __init__(self, hostname=None, username=None, password=None, language=None, timeout=None, automatic_reauthentication=False): + def __init__(self, hostname=None, username=None, password=None, language=None, timeout=None, automatic_reauthentication=False, useragent=None): # type: (Optional[str], Optional[str], Optional[str], Optional[str], Optional[float], bool) -> None - self.hostname = hostname or '%s.%s' % (ucr.get('hostname'), ucr.get('domainname')) + self.hostname = hostname or _get_fqdn() self._language = language or locale.getdefaultlocale()[0] or '' self._headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Accept': 'application/json; q=1, text/html; q=0.5; */*; q=0.1', 'Accept-Language': self._language.replace('_', '-'), 'X-Requested-With': 'XMLHttpRequest', - 'User-Agent': 'UCS/%s (univention.lib.umc/%s-errata%s)' % (ucr.get('version/version', '0.0'), ucr.get('version/patchlevel', '0'), ucr.get('version/erratalevel', '0')), + 'User-Agent': useragent or _get_useragent() } self._base_uri = '/univention/' self._timeout = timeout @@ -411,7 +427,7 @@ class Client(object): :raises ConnectionError: if :file:`/etc/machine.secret` cannot be read. """ - username = '%s$' % ucr.get('hostname') + username = '%s$' % _get_ucr().get('hostname') try: with open('/etc/machine.secret') as machine_file: password = machine_file.readline().strip() @@ -530,7 +546,7 @@ class Client(object): :raises ConnectionError: if the request cannot be send. :raises HTTPError: if an |UMC| error occurs. """ - cookie = '; '.join(['='.join(x) for x in self.cookies.iteritems()]) + cookie = '; '.join(['='.join(x) for x in self.cookies.items()]) request.headers = dict(self._headers, Cookie=cookie, **request.headers) if 'UMCSessionId' in self.cookies: request.headers['X-XSRF-Protection'] = self.cookies['UMCSessionId']