requests.auth.HTTPDigestAuth

Here are the examples of the python api requests.auth.HTTPDigestAuth taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

44 Examples 7

Example 1

Project: clam Source File: client.py
Function: init_request
    def initrequest(self, data=None):
        params = {'headers': self.initauth() }
        if self.authenticated and not self.oauth:
            params['auth'] = requests.auth.HTTPDigestAuth(self.user, self.password)
        if data:
            params['data'] = data
        params['verify'] = self.verify
        return params

Example 2

Project: SickRage Source File: qbittorrent_client.py
Function: init
    def __init__(self, host=None, username=None, password=None):

        super(qbittorrentAPI, self).__init__('qbittorrent', host, username, password)

        self.url = self.host
        self.session.auth = HTTPDigestAuth(self.username, self.password)

Example 3

Project: SiCKRAGE Source File: qbittorrent.py
    def _get_auth(self):

        try:
            self.response = sickrage.srCore.srWebSession.get(self.host,
                                                             auth=HTTPDigestAuth(self.username, self.password),
                                                             verify=bool(sickrage.srCore.srConfig.TORRENT_VERIFY_CERT))

            self.auth = self.response.text
        except Exception:
            return None

        return self.auth if not self.response.status_code == 404 else None

Example 4

Project: YCM_WIN_X86 Source File: test_requests.py
    def test_DIGEST_HTTP_200_OK_GET(self):

        auth = HTTPDigestAuth('user', 'pass')
        url = httpbin('digest-auth', 'auth', 'user', 'pass')

        r = requests.get(url, auth=auth)
        assert r.status_code == 200

        r = requests.get(url)
        assert r.status_code == 401

        s = requests.session()
        s.auth = HTTPDigestAuth('user', 'pass')
        r = s.get(url)
        assert r.status_code == 200

Example 5

Project: YCM_WIN_X86 Source File: test_requests.py
    def test_DIGEST_AUTH_RETURNS_COOKIE(self):
        url = httpbin('digest-auth', 'auth', 'user', 'pass')
        auth = HTTPDigestAuth('user', 'pass')
        r = requests.get(url)
        assert r.cookies['fake'] == 'fake_value'

        r = requests.get(url, auth=auth)
        assert r.status_code == 200

Example 6

Project: YCM_WIN_X86 Source File: test_requests.py
    def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self):
        url = httpbin('digest-auth', 'auth', 'user', 'pass')
        auth = HTTPDigestAuth('user', 'pass')
        s = requests.Session()
        s.get(url, auth=auth)
        assert s.cookies['fake'] == 'fake_value'

Example 7

Project: YCM_WIN_X86 Source File: test_requests.py
    def test_DIGEST_STREAM(self):

        auth = HTTPDigestAuth('user', 'pass')
        url = httpbin('digest-auth', 'auth', 'user', 'pass')

        r = requests.get(url, auth=auth, stream=True)
        assert r.raw.read() != b''

        r = requests.get(url, auth=auth, stream=False)
        assert r.raw.read() == b''

Example 8

Project: YCM_WIN_X86 Source File: test_requests.py
    def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):

        auth = HTTPDigestAuth('user', 'wrongpass')
        url = httpbin('digest-auth', 'auth', 'user', 'pass')

        r = requests.get(url, auth=auth)
        assert r.status_code == 401

        r = requests.get(url)
        assert r.status_code == 401

        s = requests.session()
        s.auth = auth
        r = s.get(url)
        assert r.status_code == 401

Example 9

Project: YCM_WIN_X86 Source File: test_requests.py
    def test_DIGESTAUTH_QUOTES_QOP_VALUE(self):

        auth = HTTPDigestAuth('user', 'pass')
        url = httpbin('digest-auth', 'auth', 'user', 'pass')

        r = requests.get(url, auth=auth)
        assert '"auth"' in r.request.headers['Authorization']

Example 10

Project: pimotion Source File: __init__.py
Function: get
    def __get(self, uri):
        headers = {'accept': 'application/json'}
        r = requests.get(uri, auth=HTTPDigestAuth(self.username, self.password),
                              headers=headers)
        if r.status_code != 200:
            raise CloudAppHttpError(response=r)
        return json.loads(r.text)

Example 11

Project: home-assistant Source File: mjpeg.py
    def camera_image(self):
        """Return a still image response from the camera."""
        if self._username and self._password:
            if self._authentication == HTTP_DIGEST_AUTHENTICATION:
                auth = HTTPDigestAuth(self._username, self._password)
            else:
                auth = HTTPBasicAuth(self._username, self._password)
            req = requests.get(
                self._mjpeg_url, auth=auth, stream=True, timeout=10)
        else:
            req = requests.get(self._mjpeg_url, stream=True, timeout=10)

        with closing(req) as response:
            return extract_image_from_mjpeg(response.iter_content(102400))

Example 12

Project: python_api Source File: couple.py
    def couple_cluster(self):
        conn = Connection(self.host, HTTPDigestAuth(self.adminuser, self.adminpass))
        self.marklogic = MarkLogic(conn)

        if self.couple is not None:
            for couple in self.couple:
                print("{0}: couple with {1}...".format(self.host,couple))
                altconn = Connection(couple,
                                     HTTPDigestAuth(self.couple_user,
                                                    self.couple_pass))
                altml = MarkLogic(altconn)
                altcluster = altml.cluster()
                cluster = self.marklogic.cluster()
                cluster.couple(altcluster)

        print("Finished")

Example 13

Project: python_api Source File: load_content.py
    def load_data(self):
        simpleapp = SimpleApplication(tc.appname, tc.port)

        conn = Connection(tc.hostname, HTTPDigestAuth(tc.admin, tc.password))
        hostname = Host.list(conn)[0]
        exampleapp = simpleapp.create(conn, hostname)

        loader = MLCPLoader()
        loader.load_directory(conn, exampleapp['content'],
                              "data",
                              collections=["example1"], prefix="/test/data1")

Example 14

Project: python_api Source File: properties.py
    def connect(self, args):
        try:
            adminuser, adminpass = re.split(":", args['credentials'])
        except ValueError:
            print ("--credentials value must be 'user:password':",
                   args['credentials'])
            sys.exit(1)

        if args['debug']:
            logging.basicConfig(level=logging.WARNING)
            logging.getLogger("requests").setLevel(logging.WARNING)
            logging.getLogger("marklogic").setLevel(logging.DEBUG)

        self.connection \
          = Connection(args['hostname'], HTTPDigestAuth(adminuser, adminpass))
        self.mls = MarkLogic(self.connection)
        self.args = args

Example 15

Project: python_api Source File: cluster.py
    def couple(self, args, config, connection):
        cluster = LocalCluster(connection=connection)
        cluster.read()

        try:
            username, password = re.split(":", args['couple_credentials'])
        except ValueError:
            print ("--couple-credentials value must be 'user:password':",
                   args['couple_credentials'])
            sys.exit(1)

        altconn = Connection(args['host'], HTTPDigestAuth(username, password))
        altcluster = LocalCluster(connection=altconn)

        cluster.couple(altcluster, connection=connection,
                       other_cluster_connection=altconn)

Example 16

Project: release-tools Source File: update_reviews.py
Function: init
    def __init__(self, project, user=None, password=None,
                 updating_review_cb=lambda x: None):
        self.base_url = 'https://review.openstack.org/'

        if not (user and password):
            config = _read_config()
            user = config['username']
            password = config['password']
            self.base_url = config['url']

        self.auth = auth.HTTPDigestAuth(user, password)

        self.project = project
        self.updating_review_cb = updating_review_cb

Example 17

Project: vdirsyncer Source File: http.py
def prepare_auth(auth, username, password):
    if username and password:
        if auth == 'basic' or auth is None:
            return (username, password)
        elif auth == 'digest':
            from requests.auth import HTTPDigestAuth
            return HTTPDigestAuth(username, password)
        elif auth == 'guess':
            try:
                from requests_toolbelt.auth.guess import GuessAuth
            except ImportError:
                raise exceptions.UserError(
                    'Your version of requests_toolbelt is too '
                    'old for `guess` authentication. At least '
                    'version 0.4.0 is required.'
                )
            else:
                return GuessAuth(username, password)
        else:
            raise exceptions.UserError('Unknown authentication method: {}'
                                       .format(auth))
    elif auth:
        raise exceptions.UserError('You need to specify username and password '
                                   'for {} authentication.'.format(auth))
    else:
        return None

Example 18

Project: SickRage Source File: requests_transport.py
Function: request
    def request(self, host, handler, request_body, verbose=0):
        """Replace the xmlrpc request function.

        Process xmlrpc request via requests library.

        Args:
            host: Target host
            handler: Target PRC handler.
            request_body: XML-RPC request body.
            verbose: Debugging flag.

        Returns:
            Parsed response.

        Raises:
            RequestException: Error in requests
        """
        if verbose:
            self._debug()

        if not self._check_ssl_cert:
            disable_warnings()

        headers = {'User-Agent': self.user_agent, 'Content-Type': 'text/xml', }

        # Need to be done because the schema(http or https) is lost in
        # xmlrpc.Transport's init.
        if self._use_https:
            url = "https://{host}/{handler}".format(host=host, handler=handler)
        else:
            url = "http://{host}/{handler}".format(host=host, handler=handler)

        # TODO Construct kwargs query instead
        try:
            if self._authtype == "basic":
                response = requests.post(
                    url,
                    data=request_body,
                    headers=headers,
                    verify=self._check_ssl_cert,
                    auth=HTTPBasicAuth(
                        self._username, self._password),
                    proxies=self._proxies)
            elif self._authtype == "digest":
                response = requests.post(
                    url,
                    data=request_body,
                    headers=headers,
                    verify=self._check_ssl_cert,
                    auth=HTTPDigestAuth(
                        self._username, self._password),
                    proxies=self._proxies)
            else:
                response = requests.post(
                    url,
                    data=request_body,
                    headers=headers,
                    verify=self._check_ssl_cert,
                    proxies=self._proxies)

            response.raise_for_status()
        except RequestException as error:
            raise xmlrpc_client.ProtocolError(url,
                                              error.message,
                                              traceback.format_exc(),
                                              response.headers)

        return self.parse_response(response)

Example 19

Project: pygerrit Source File: rest_example.py
def _main():
    descr = 'Send request using Gerrit HTTP API'
    parser = argparse.ArgumentParser(
        description=descr,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-g', '--gerrit-url', dest='gerrit_url',
                        required=True,
                        help='gerrit server url')
    parser.add_argument('-b', '--basic-auth', dest='basic_auth',
                        action='store_true',
                        help='use basic auth instead of digest')
    if _kerberos_support:
        parser.add_argument('-k', '--kerberos-auth', dest='kerberos_auth',
                            action='store_true',
                            help='use kerberos auth')
    parser.add_argument('-u', '--username', dest='username',
                        help='username')
    parser.add_argument('-p', '--password', dest='password',
                        help='password')
    parser.add_argument('-n', '--netrc', dest='netrc',
                        action='store_true',
                        help='Use credentials from netrc')
    parser.add_argument('-v', '--verbose', dest='verbose',
                        action='store_true',
                        help='enable verbose (debug) logging')

    options = parser.parse_args()

    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=level)

    if _kerberos_support and options.kerberos_auth:
        if options.username or options.password \
                or options.basic_auth or options.netrc:
            parser.error("--kerberos-auth may not be used together with "
                         "--username, --password, --basic-auth or --netrc")
        auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
    elif options.username and options.password:
        if options.netrc:
            logging.warning("--netrc option ignored")
        if options.basic_auth:
            auth = HTTPBasicAuth(options.username, options.password)
        else:
            auth = HTTPDigestAuth(options.username, options.password)
    elif options.netrc:
        if options.basic_auth:
            auth = HTTPBasicAuthFromNetrc(url=options.gerrit_url)
        else:
            auth = HTTPDigestAuthFromNetrc(url=options.gerrit_url)
    else:
        auth = None

    rest = GerritRestAPI(url=options.gerrit_url, auth=auth)

    try:
        changes = rest.get("/changes/?q=owner:self%20status:open")
        logging.info("%d changes", len(changes))
        for change in changes:
            logging.info(change['change_id'])
    except RequestException as err:
        logging.error("Error: %s", str(err))

Example 20

Project: robotframework-requests Source File: RequestsKeywords.py
    def create_digest_session(self, alias, url, auth, headers={}, cookies=None,
                              timeout=None, proxies=None, verify=False,
                              debug=0, max_retries=3,backoff_factor=0.10, disable_warnings=0):
        """ Create Session: create a HTTP session to a server

        ``url`` Base url of the server

        ``alias`` Robot Framework alias to identify the session

        ``headers`` Dictionary of default headers

        ``auth`` ['DOMAIN', 'username', 'password'] for NTLM Authentication

        ``timeout`` Connection timeout

        ``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication

        ``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.
                 Defaults to False.

        ``debug`` Enable http verbosity option more information
                https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel

        ``max_retries`` The maximum number of retries each connection should attempt.
        
        ``backoff_factor`` The pause between for each retry
        
        ``disable_warnings`` Disable requests warning useful when you have large number of testcases
        """
        digest_auth = requests.auth.HTTPDigestAuth(*auth) if auth else None

        return self._create_session(
            alias,
            url,
            headers,
            cookies,
            digest_auth,
            timeout,
            max_retries,
            backoff_factor,
            proxies,
            verify,
            debug,
            disable_warnings)

Example 21

Project: conn-check Source File: checks.py
def make_http_check(url, method='GET', expected_code=200, **kwargs):
    subchecks = []
    host, port, scheme = extract_host_port(url)
    proxy_url = kwargs.get('proxy_url')
    proxy_host = kwargs.get('proxy_host')
    proxy_port = int(kwargs.get('proxy_port', 8000))
    timeout = kwargs.get('timeout', None)
    expected_code = int(expected_code)

    if proxy_host:
        subchecks.append(make_tcp_check(proxy_host, proxy_port,
                                        timeout=timeout))
    else:
        subchecks.append(make_tcp_check(host, port, timeout=timeout))

    @inlineCallbacks
    def do_request():
        proxies = {}
        if proxy_url:
            proxies['http'] = proxies['https'] = proxy_url
        elif proxy_host:
            proxies['http'] = proxies['https'] = '{}:{}'.format(
                proxy_host, proxy_port)

        headers = kwargs.get('headers')
        body = kwargs.get('body')
        disable_tls_verification = kwargs.get('disable_tls_verification',
                                              False)
        allow_redirects = kwargs.get('allow_redirects', False)
        params = kwargs.get('params')
        cookies = kwargs.get('cookies')
        auth = kwargs.get('auth')
        digest_auth = kwargs.get('digest_auth')

        args = {
            'method': method,
            'url': url,
            'verify': not disable_tls_verification,
            'timeout': timeout,
            'allow_redirects': allow_redirects,
        }
        if headers:
            args['headers'] = headers
        if body:
            args['data'] = body
        if proxies:
            args['proxies'] = proxies
        if params:
            args['params'] = params
        if cookies:
            args['cookies'] = cookies
        if auth:
            args['auth'] = auth
        if digest_auth:
            args['auth'] = HTTPDigestAuth(digest_auth)

        if disable_tls_verification:
            disable_warnings()

        with Session() as session:
            request = session.request(**args)

            response = yield request
            if response.status_code != expected_code:
                raise RuntimeError(
                    "Unexpected response code: {}".format(
                        response.status_code))

    subchecks.append(make_check('', do_request,
                     info='{} {}'.format(method, url)))

    return add_check_prefix('http:{}'.format(url),
                            sequential_check(subchecks))

Example 22

Project: vmnetx Source File: source.py
    def __init__(self, url, scheme=None, username=None, password=None,
            buffer_size=64 << 10):
        if scheme == 'Basic':
            self._auth = (username, password)
        elif scheme == 'Digest':
            self._auth = requests.auth.HTTPDigestAuth(username, password)
        elif scheme is None:
            self._auth = None
        else:
            raise ValueError('Unknown authentication scheme')

        self.url = url
        self._offset = 0
        self._closed = False
        self._buffer = ''
        self._buffer_offset = 0
        self._buffer_size = buffer_size
        self._session = get_requests_session()

        # Debugging
        self._last_case = None
        self._last_network = None

        # Perform HEAD request
        try:
            resp = self._session.head(self.url, auth=self._auth)

            # Check for missing credentials
            if resp.status_code == 401:
                # Assumes a single challenge.
                scheme, parameters = resp.headers['WWW-Authenticate'].split(
                        None, 1)
                if scheme != 'Basic' and scheme != 'Digest':
                    raise SourceError('Server requested unknown ' +
                            'authentication scheme: %s' % scheme)
                host = urlsplit(self.url).netloc
                for param in parameters.split(', '):
                    match = re.match('^realm=\"([^"]*)\"$', param)
                    if match:
                        raise NeedAuthentication(host, match.group(1), scheme)
                raise SourceError('Unknown authentication realm')

            # Check for other errors
            resp.raise_for_status()
            # 2xx codes other than 200 are unexpected
            if resp.status_code != 200:
                raise SourceError('Unexpected status code %d' %
                        resp.status_code)

            # Store object length
            try:
                self.length = int(resp.headers['Content-Length'])
            except (IndexError, ValueError):
                raise SourceError('Server did not provide Content-Length')

            # Store validators
            self.etag = self._get_etag(resp)
            self.last_modified = self._get_last_modified(resp)

            # Record cookies
            if hasattr(self._session.cookies, 'extract_cookies'):
                # CookieJar
                self.cookies = tuple(c for c in self._session.cookies)
            else:
                # dict (requests < 0.12.0)
                parsed = urlsplit(self.url)
                self.cookies = tuple(Cookie(version=0,
                        name=name, value='"%s"' % value,
                        port=None, port_specified=False,
                        domain=parsed.netloc, domain_specified=False,
                        domain_initial_dot=False,
                        path=parsed.path, path_specified=True,
                        secure=False, expires=None, discard=True,
                        comment=None, comment_url=None, rest={})
                        for name, value in self._session.cookies.iteritems())
        except requests.exceptions.RequestException, e:
            raise SourceError(str(e))

Example 23

Project: cylc Source File: base_client.py
    def get_data_from_url_with_requests(self, url, json_data, method=None):
        import requests
        username, password = self._get_auth()
        auth = requests.auth.HTTPDigestAuth(username, password)
        if not hasattr(self, "session"):
            self.session = requests.Session()
        if method is None:
            method = self.METHOD
        if method == self.METHOD_POST:
            session_method = self.session.post
        else:
            session_method = self.session.get
        try:
            ret = session_method(
                url,
                json=json_data,
                verify=self._get_verify(),
                proxies={},
                headers=self._get_headers(),
                auth=auth,
                timeout=self.timeout
            )
        except requests.exceptions.SSLError as exc:
            if "unknown protocol" in str(exc) and url.startswith("https:"):
                # Server is using http rather than https, for some reason.
                sys.stderr.write(WARNING_NO_HTTPS_SUPPORT.format(exc))
                return self.get_data_from_url_with_requests(
                    url.replace("https:", "http:", 1), json_data)
            if cylc.flags.debug:
                import traceback
                traceback.print_exc()
            raise ConnectionError(url, exc)
        except Exception as exc:
            if cylc.flags.debug:
                import traceback
                traceback.print_exc()
            raise ConnectionError(url, exc)
        if ret.status_code == 401:
            raise ConnectionDeniedError(url, self.prog_name,
                                        self.ACCESS_DESCRIPTION)
        if ret.status_code >= 400:
            from cylc.network.https.util import get_exception_from_html
            exception_text = get_exception_from_html(ret.text)
            if exception_text:
                sys.stderr.write(exception_text)
            else:
                sys.stderr.write(ret.text)
        try:
            ret.raise_for_status()
        except Exception as exc:
            if cylc.flags.debug:
                import traceback
                traceback.print_exc()
            raise ConnectionError(url, exc)
        try:
            return ret.json()
        except ValueError:
            return ret.text

Example 24

Project: commcare-hq Source File: import_app.py
Function: handle
    def handle(self, *args, **options):
        domain, app_id = args

        username = self._get_required_option('username', options)
        target_domain = self._get_required_option('to_domain', options)

        name = options['to_name']
        url_base = options['url']
        password = options['password']
        if not password:
            password = getpass("Please enter the password for '{}': ".format(username))

        url = reverse('app_source', kwargs={'domain': domain, 'app_id': app_id})
        full_url = '{}{}'.format(url_base, url)
        resp = requests.get(full_url, auth=HTTPDigestAuth(username, password))
        if not resp.status_code == 200:
            return "Command Failed: {}: {}".format(resp.status_code, resp.text)

        app_source = resp.json()
        if not name:
            name = app_source['name']
        import_app(app_source, target_domain, {'name': name})

Example 25

Project: pycarddav Source File: carddav.py
Function: init
    def __init__(self, resource, debug='', user='', passwd='',
                 verify=True, write_support=False, auth='basic'):
        #shutup urllib3
        urllog = logging.getLogger('requests.packages.urllib3.connectionpool')
        urllog.setLevel(logging.CRITICAL)
        urllog = logging.getLogger('urllib3.connectionpool')
        urllog.setLevel(logging.CRITICAL)

        # activate pyopenssl if available
        try:
            import urllib3.contrib.pyopenssl
        except ImportError:
            pass
        else:
            urllib3.contrib.pyopenssl.inject_into_urllib3()

        split_url = urlparse.urlparse(resource)
        url_tuple = namedtuple('url', 'resource base path')
        self.url = url_tuple(resource,
                             split_url.scheme + '://' + split_url.netloc,
                             split_url.path)
        self.debug = debug
        self.session = requests.session()
        self.write_support = write_support
        self._settings = {'verify': verify}
        if auth == 'basic':
            self._settings['auth'] = (user, passwd,)
        if auth == 'digest':
            from requests.auth import HTTPDigestAuth
            self._settings['auth'] = HTTPDigestAuth(user, passwd)
        self._default_headers = {"User-Agent": "pyCardDAV"}

        headers = self.headers
        headers['Depth'] = '1'
        response = self.session.request('OPTIONS',
                                        self.url.resource,
                                        headers=headers,
                                        **self._settings)
        response.raise_for_status()   # raises error on not 2XX HTTP status code
        if 'addressbook' not in response.headers.get('DAV', ''):
            raise Exception("URL is not a CardDAV resource")

Example 26

Project: home-assistant Source File: rest.py
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the REST binary sensor."""
    name = config.get(CONF_NAME)
    resource = config.get(CONF_RESOURCE)
    method = config.get(CONF_METHOD)
    payload = config.get(CONF_PAYLOAD)
    verify_ssl = config.get(CONF_VERIFY_SSL)
    username = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    headers = config.get(CONF_HEADERS)
    sensor_class = config.get(CONF_SENSOR_CLASS)
    value_template = config.get(CONF_VALUE_TEMPLATE)
    if value_template is not None:
        value_template.hass = hass

    if username and password:
        if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
            auth = HTTPDigestAuth(username, password)
        else:
            auth = HTTPBasicAuth(username, password)
    else:
        auth = None

    rest = RestData(method, resource, auth, headers, payload, verify_ssl)
    rest.update()

    if rest.data is None:
        _LOGGER.error("Unable to fetch REST data from %s", resource)
        return False

    add_devices([RestBinarySensor(
        hass, rest, name, sensor_class, value_template)])

Example 27

Project: home-assistant Source File: generic.py
Function: init
    def __init__(self, hass, device_info):
        """Initialize a generic camera."""
        super().__init__()
        self.hass = hass
        self._authentication = device_info.get(CONF_AUTHENTICATION)
        self._name = device_info.get(CONF_NAME)
        self._still_image_url = device_info[CONF_STILL_IMAGE_URL]
        self._still_image_url.hass = hass
        self._limit_refetch = device_info[CONF_LIMIT_REFETCH_TO_URL_CHANGE]

        username = device_info.get(CONF_USERNAME)
        password = device_info.get(CONF_PASSWORD)

        if username and password:
            if self._authentication == HTTP_DIGEST_AUTHENTICATION:
                self._auth = HTTPDigestAuth(username, password)
            else:
                self._auth = aiohttp.BasicAuth(username, password=password)
        else:
            self._auth = None

        self._last_url = None
        self._last_image = None

Example 28

Project: home-assistant Source File: rest.py
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the RESTful sensor."""
    name = config.get(CONF_NAME)
    resource = config.get(CONF_RESOURCE)
    method = config.get(CONF_METHOD)
    payload = config.get(CONF_PAYLOAD)
    verify_ssl = config.get(CONF_VERIFY_SSL)
    username = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    headers = config.get(CONF_HEADERS)
    unit = config.get(CONF_UNIT_OF_MEASUREMENT)
    value_template = config.get(CONF_VALUE_TEMPLATE)
    if value_template is not None:
        value_template.hass = hass

    if username and password:
        if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
            auth = HTTPDigestAuth(username, password)
        else:
            auth = HTTPBasicAuth(username, password)
    else:
        auth = None
    rest = RestData(method, resource, auth, headers, payload, verify_ssl)
    rest.update()

    if rest.data is None:
        _LOGGER.error('Unable to fetch REST data')
        return False

    add_devices([RestSensor(hass, rest, name, unit, value_template)])

Example 29

Project: restsh Source File: restshlib.py
Function: set_auth
    def set_auth(self, username, password, typ="basic"):
        if typ == "basic":
            self.auth = HTTPBasicAuth(username, password)
        elif typ == "digest":
            self.auth = HTTPDigestAuth(username, password)

Example 30

Project: httpie Source File: builtin.py
Function: get_auth
    def get_auth(self, username, password):
        return requests.auth.HTTPDigestAuth(username, password)

Example 31

Project: pmatic Source File: devicetr64.py
Function: execute
    def execute(self, uri, namespace, action, timeout=2, **kwargs):
        """Executes a given action with optional arguments.

        The execution of an action of an UPnP/TR64 device needs more than just the name of an action. It needs the
        control URI which is called to place the action and also the namespace aka service type is needed. The
        namespace defines the scope or service type of the given action, the same action name can appear in different
        namespaces.

        The way how to obtain the needed information's is either through the docuementation of the vendor of the
        device. Or through a discovery requests which return's the URL to the root device description XML.

        :param str uri: the control URI, for example ``/upnp/control/hosts``
        :param str namespace: the namespace for the given action, for example ``urn:dslforum-org:service:Hosts:1``
        :param str action: the name of the action to call, for example ``GetGenericHostEntry``
        :param float timeout: the timeout to wait for the action to be executed
        :param kwargs: optional arguments for the given action, depends if the action needs parameter. The arguments
            are given as dict where the key is the parameter name and the value the value of the parameter.
        :type kwargs: dict[str, str]
        :return: returns the results of the action, if any. The results are structured as dict where the key is the
            name of the result argument and the value is the value of the result.
        :rtype: dict[str,str]
        :raises ValueError: if parameters are not set correctly
        :raises requests.exceptions.ConnectionError: when the action can not be placed on the device
        :raises requests.exceptions.ConnectTimeout: when download time out

        Example:

        ::

            device = DeviceTR64(...)
            device.execute("/upnp/control/hosts", "urn:dslforum-org:service:Hosts:1",
                "GetGenericHostEntry", {"NewIndex": 1})
            {'NewActive': '0', 'NewIPAddress': '192.168.0.23', 'NewMACAddress': '9C:20:7B:E7:FF:5F',
                'NewInterfaceType': 'Ethernet', 'NewHostName': 'Apple-TV', 'NewAddressSource': 'DHCP',
                'NewLeaseTimeRemaining': '0'}

        .. seealso::

            `Additional short explanation of the UPnP protocol <http://www.upnp-hacks.org/upnp.html>`_

            :class:`~simpletr64.Discover`, :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`,
            :meth:`~simpletr64.DeviceTR64.loadSCPD`
        """

        if not uri:
            raise ValueError("No action URI has been defined.")

        if not namespace:
            raise ValueError("No namespace has been defined.")

        if not action:
            raise ValueError("No action has been defined.")

        # soap headers
        header = {'Content-Type': 'text/xml; charset="UTF-8"',
                  'Soapaction': '"' + namespace + "#" + action + '"'}

        # build SOAP body
        body = '''<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <s:Header/>
    <s:Body>\n'''

        body += "        <u:" + action + ' xmlns="' + namespace + '">\n'

        arguments = {}

        for key in kwargs.keys():
            body += "            <" + key + ">" + str(kwargs[key]) + "</" + key + ">\n"
            arguments[key] = str(kwargs[key])

        body += "        </u:" + action + ">\n"
        body += '''</s:Body>
</s:Envelope>'''

        # setup proxies
        proxies = {}
        if self.__httpsProxy:
            proxies = {"https": self.__httpsProxy}

        if self.__httpProxy:
            proxies = {"http": self.__httpProxy}

        # setup authentication
        auth = None
        if self.__password:
            auth = HTTPDigestAuth(self.__username, self.__password)

        # build the URL
        location = self.__protocol + "://" + self.__hostname + ":" + str(self.port) + uri

        # Post http request
        request = requests.post(location, data=body, headers=header, auth=auth, proxies=proxies, timeout=float(timeout))

        if request.status_code != 200:
            errorStr = DeviceTR64._extractErrorString(request)
            raise ValueError('Could not execute "' + action + str(arguments) + '": ' + str(request.status_code) +
                             ' - ' + request.reason + " -- " + errorStr)

        # parse XML return
        try:
            root = ET.fromstring(request.text.encode('utf-8'))
        except Exception as e:
            raise ValueError("Can not parse results for the action: " + str(e))

        # iterate in the XML structure to get the action result
        actionNode = root[0][0]

        # we need to remove XML namespace for the action node
        namespaceLength = len(namespace) + 2  # add braces
        tag = actionNode.tag[namespaceLength:]

        if tag != (action + "Response"):
            raise ValueError('Soap result structure is wrong, expected action "' + action +
                             'Response" got "' + tag + '".')

        # pack all the received results
        results = {}

        for resultNode in actionNode:
            results[resultNode.tag] = resultNode.text

        return results

Example 32

Project: pmatic Source File: devicetr64.py
    def _loadSCPD(self, serviceType, timeout):
        """Internal method to load the action definitions.

        :param str serviceType: the service type to load
        :param int timeout: the timeout for downloading
        """

        if serviceType not in self.__deviceServiceDefinitions.keys():
            raise ValueError("Can not load SCPD, no service type defined for: " + serviceType)

        if "scpdURL" not in self.__deviceServiceDefinitions[serviceType].keys():
            raise ValueError("No SCPD URL defined for: " + serviceType)

        # remove actions for given service type
        self.__deviceSCPD.pop(serviceType, None)

        uri = self.__deviceServiceDefinitions[serviceType]["scpdURL"]

        # setup proxies
        proxies = {}
        if self.__httpsProxy:
            proxies = {"https": self.__httpsProxy}

        if self.__httpProxy:
            proxies = {"http": self.__httpProxy}

        # setup authentication
        auth = None
        if self.__password:
            auth = HTTPDigestAuth(self.__username, self.__password)

        # build the URL
        location = self.__protocol + "://" + self.__hostname + ":" + str(self.port) + uri

        # some devices response differently without a User-Agent
        headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-2"}

        # http request
        request = requests.get(location, auth=auth, proxies=proxies, headers=headers, timeout=timeout)

        if request.status_code != 200:
            errorStr = DeviceTR64._extractErrorString(request)
            raise ValueError('Could not load SCPD for "' + serviceType + '" from ' + location + ': ' +
                             str(request.status_code) + ' - ' + request.reason + " -- " + errorStr)

        data = request.text.encode('utf-8')
        if len(data) == 0:
            return

        # parse XML return
        try:
            root = ET.fromstring(data)
        except Exception as e:
            raise ValueError("Can not parse SCPD content for '" + serviceType + "' from '" + location + "': " + str(e))

        actions = {}
        variableTypes = {}
        variableParameterDict = {}

        # iterate through the full XML tree
        for element in root.getchildren():
            tagName = element.tag.lower()

            # go deeper for action lists
            if tagName.endswith("actionlist"):
                # remember the actions and where a specific variable gets referenced
                self._parseSCPDActions(element, actions, variableParameterDict)

            # go deeper for the variable declarations
            elif tagName.endswith("servicestatetable"):
                self._parseSCPDVariableTypes(element, variableTypes)

        # everything have been parsed now merge the variable declarations into the action parameters
        for name in variableParameterDict.keys():
            if name not in variableTypes.keys():
                raise ValueError("Variable reference in action can not be resolved: " + name)

            # iterate through all arguments where this variable have been referenced
            for argument in variableParameterDict[name]:
                # fill in the type of this variable/argument
                argument["dataType"] = variableTypes[name]["dataType"]

                # if the variable declaration includes a default value add it to the action parameter as well
                if "defaultValue" in variableTypes[name].keys():
                    argument["defaultValue"] = variableTypes[name]["defaultValue"]

        self.__deviceSCPD[serviceType] = actions

Example 33

Project: python_api Source File: backup-database.py
    def backup_databases(self):
        conn = Connection(self.host,
                          HTTPDigestAuth(self.adminuser, self.adminpass))
        self.marklogic = MarkLogic(conn)

        actual_databases = self.marklogic.databases()

        if self.databases is None:
            self.databases = actual_databases

        if self.backup_root is None:
            raise UnsupportedOperation("You must specify the backup root.")

        if self.max_parallel <= 0:
            self.max_parallel = 1

        for dbname in self.databases:
            if not dbname in actual_databases:
                raise UnsupportedOperation("Database does not exist: {0}"
                                           .format(dbname))

        maxp = self.max_parallel
        running = 0
        done = False
        queue = self.databases
        status_list = {}
        min_wait = 5
        max_wait = 30
        wait_incr = 5
        wait = min_wait

        while not done:
            done = True

            while len(queue) > 0 and running < maxp:
                dbname = queue.pop(0)
                running += 1
                done = False

                print("Backing up {0}".format(dbname))
                if not self.dry_run:
                    db = self.marklogic.database(dbname)
                    bkp = db.backup(self.backup_root + dbname, connection=conn)
                    status_list[dbname] = bkp
                    response = bkp.status()
                    if response['status'] != 'in-progress':
                        print("{0}: {1}".format(dbname, response['status']))

            if self.dry_run:
                if running > 0 or len(queue) > 0:
                    print("{0} backups in dry-run; {1} in queue..."
                          .format(running, len(queue)))
                    running = 0
            else:
                if len(status_list) > 0:
                    new_list = {}
                    for dbname in status_list:
                        bkp = status_list[dbname]
                        response = bkp.status()
                        print("{0}: {1}".format(dbname, response['status']))
                        if response['status'] == 'in-progress':
                            done = False
                            new_list[dbname] = bkp
                        else:
                            running -= 1
                            wait = min_wait

                    done = done and len(queue) == 0

                    if not done:
                        status_list = new_list
                        if running < maxp and len(queue) != 0:
                            print("Running: {0} backups running; {1} in queue..."
                                  .format(running, len(queue)))
                            wait = min_wait
                            print("")
                        else:
                            print("Waiting {0}s: {1} backups running; {2} in queue..."
                                  .format(wait, running, len(queue)))
                            time.sleep(wait)
                            if wait < max_wait:
                                wait += wait_incr
                            print("")

Example 34

Project: python_api Source File: create-forests.py
    def create_forests(self):
        conn = Connection(self.host,
                          HTTPDigestAuth(self.adminuser, self.adminpass))
        self.marklogic = MarkLogic(conn)

        if self.failhost is None and self.failover != "none":
            print("Invalid configuration, specify failover-host for failover:",
                  self.failover)
            sys.exit(1)

        if self.hosts is None:
            host_names = self.marklogic.hosts()
        else:
            host_names = self.hosts

        exists = []
        host_index = 0
        for host_name in host_names:
            host_index += 1
            for count in range(1,self.forests + 1):
                name = self.prefix + "-" + str(host_index) + "-" + str(count)
                forest = Forest(name, host_name, conn)
                if forest.exists():
                    exists.append(host_name + ":" + name)

        if len(exists) != 0:
            print("Abort: forest(s) already exist:")
            for f in exists:
                print("   ", f)
            sys.exit(1)

        host_index = 0
        for host_name in host_names:
            host_index += 1
            for count in range(self.start,self.start + self.forests):
                name = self.prefix + "-" + str(host_index) + "-" + str(count)
                forest = Forest(name, host_name, conn)
                if self.data_dir is not None:
                    forest.set_data_directory(self.data_dir)
                if self.large_data_dir is not None:
                    forest.set_large_data_directory(self.large_data_dir)
                if self.fast_data_dir is not None:
                    forest.set_fast_data_directory(self.fast_data_dir)

                if self.failhost is not None:
                    forest.set_failover(self.failover)
                    forest.set_failover_host_names(self.failhost)

                if self.database is not None:
                    forest.set_database(self.database)

                print("Create forest " + name + " on " + host_name)
                if self.dry_run:
                    print(json.dumps(forest.marshal(), sort_keys=True, indent=2))
                else:
                    forest.create()

        print("Finished")

Example 35

Project: python_api Source File: docker-cluster.py
    def setup_cluster(self):
        if self.couple is not None and self.couple_pass is None:
            self.couple_pass = self.adminpass
            self.couple_user = self.adminuser

        self.load_blacklist()
        self.find_containers()

        if not self.container_list:
            print("There must be at least one unused container running.")
            sys.exit(1)

        if self.localimage:
            ps = os.popen("docker exec " + self.container_list[0] + " ip route")
            for line in ps.readlines():
                match = re.match("^default via (\S+)", line)
                if match:
                    self.localip = match.group(1)
            if self.localip is None:
                print("Cannot find IP address of localhost!?")
                sys.exit(1)

        # Find the bootstrap image

        if self.localimage:
            pass
        else:
            self.bootimage = self.pick_image(self.bootimage)
            if self.bootimage in self.container_list:
                self.container_list.remove(self.bootimage)

        self.cluster_list = self.pick_containers()

        self.display_info()
        #sys.exit(1)

        # Initialize the bootstrap image, if necessary

        if self.localimage:
            bootip = self.localip
        else:
            bootip = self.ipaddr[self.bootimage]

        if self.ml_init(bootip, self.bootimage):
            self.ml_security(bootip, self.bootimage)

        conn = Connection(bootip, HTTPDigestAuth(self.adminuser, self.adminpass))
        self.marklogic = MarkLogic(conn)

        for container in self.cluster_list:
            if container == self.bootimage:
                continue
            ip = self.ipaddr[container]
            self.ml_init(ip, container)
            self.ml_join(bootip, ip)

        if self.name is not None:
            print("{0}: rename cluster...".format(bootip))
            cluster = self.marklogic.cluster()
            cluster.set_cluster_name(self.name)
            cluster.update()

        if self.couple is not None:
            for couple in self.couple:
                print("{0}: couple with {1}...".format(bootip, couple))
                altconn = Connection(couple,
                                     HTTPDigestAuth(self.couple_user,
                                                    self.couple_pass))
                altml = MarkLogic(altconn)
                altcluster = altml.cluster()
                cluster = self.marklogic.cluster()
                cluster.couple(altcluster)

        print("Finished")

Example 36

Project: python_api Source File: mldbmirror.py
    def connect(self, args):
        """Connect to the server"""
        self.path = os.path.abspath(args['path'])
        self.loadconfig(self.path)

        if args['credentials'] is not None:
            cred = args['credentials']
        else:
            if 'user' in self.config and 'pass' in self.config:
                cred = self.config['user'] + ":" + self.config['pass']
            else:
                cred = None

        try:
            adminuser, adminpass = re.split(":", cred)
        except ValueError:
            raise RuntimeError("Invalid credentials (must be user:pass): {}" \
                                   .format(args['credentials']))

        if args['debug']:
            logging.basicConfig(level=logging.WARNING)
            logging.getLogger("requests").setLevel(logging.INFO)
            logging.getLogger("marklogic").setLevel(logging.DEBUG)

        self.batchsize = args['batchsize']
        self.database = args['database']
        self.dryrun = args['dryrun']
        self.list = args['list']
        self.mirror = args['mirror']
        self.regex = args['regex']
        self.root = args['root']
        self.threshold = args['threshold']
        self.verbose = args['verbose']

        if self.list and self.regex:
            raise RuntimeError("You must not specify both --regex and --list")

        if self.root.endswith("/"):
            self.root = self.root[0:len(self.root)-1]

        if args['hostname'] is None:
            if 'host' in self.config:
                self.hostname = self.config['host']
                if 'port' in self.config:
                    self.port = self.config['port']
                else:
                    self.port = 8000
                if 'management-port' in self.config:
                    self.management_port = self.config['management-port']
                else:
                    self.management_port = 8002
        else:
            parts = args['hostname'].split(":")
            self.hostname = parts.pop(0)
            self.management_port = 8002
            self.port = 8000
            if parts:
                self.management_port = parts.pop(0)
            if parts:
                self.port = parts.pop(0)

        self.connection \
          = Connection(self.hostname, HTTPDigestAuth(adminuser, adminpass), \
                           port=self.port, management_port=self.management_port)

        self.utils = ClientUtils(self.connection)

Example 37

Project: python_api Source File: restore-database.py
    def restore_databases(self):
        conn = Connection(self.host,
                          HTTPDigestAuth(self.adminuser, self.adminpass))
        self.marklogic = MarkLogic(conn)

        actual_databases = self.marklogic.databases()

        if self.databases is None:
            self.databases = actual_databases

        if self.restore_root is None:
            raise UnsupportedOperation("You must specify the restore root.")

        if self.max_parallel <= 0:
            self.max_parallel = 1

        for dbname in self.databases:
            if not dbname in actual_databases:
                raise UnsupportedOperation("Database does not exist: {0}"
                                           .format(dbname))

        maxp = self.max_parallel
        running = 0
        done = False
        queue = self.databases
        status_list = {}
        min_wait = 5
        max_wait = 30
        wait_incr = 5
        wait = min_wait

        while not done:
            done = True

            while len(queue) > 0 and running < maxp:
                dbname = queue.pop(0)
                running += 1
                done = False

                print("Restoring {0}".format(dbname))
                if not self.dry_run:
                    db = self.marklogic.database(dbname)
                    rst = db.restore(self.restore_root + dbname, connection=conn)
                    status_list[dbname] = rst
                    response = rst.status()
                    for forest in response['forest']:
                        if forest['status'] != 'in-progress':
                            print("Forest {0}: {1}"
                                  .format(forest['forest-name'],
                                          forest['status']))

            if self.dry_run:
                if running > 0 or len(queue) > 0:
                    print("{0} restores in dry-run; {1} in queue..."
                          .format(running, len(queue)))
                    running = 0
            else:
                if len(status_list) > 0:
                    new_list = {}
                    for dbname in status_list:
                        rst = status_list[dbname]
                        response = rst.status()
                        fdone = True
                        for forest in response['forest']:
                            print("Forest {0}: {1}"
                                  .format(forest['forest-name'],
                                          forest['status']))
                            if forest['status'] == 'in-progress':
                                fdone = False
                        if not fdone:
                            done = False
                            new_list[dbname] = rst
                        else:
                            running -= 1
                            wait = min_wait

                    done = done and len(queue) == 0

                    if not done:
                        status_list = new_list
                        if running < maxp and len(queue) != 0:
                            print("Running: {0} restores running; {1} in queue..."
                                  .format(running, len(queue)))
                            wait = min_wait
                            print("")
                        else:
                            print("Waiting {0}s: {1} restores running; {2} in queue..."
                                  .format(wait, running, len(queue)))
                            time.sleep(wait)
                            if wait < max_wait:
                                wait += wait_incr
                            print("")

Example 38

Project: python_api Source File: connection.py
Function: make_connection
    @classmethod
    def make_connection(cls, host, username, password):
        return Connection(host, HTTPDigestAuth(username, password))

Example 39

Project: python_api Source File: mma.py
    def run(self, argv):
        command = None
        artifact = None

        # This is an odd program. The parser used for each line depends on
        # the command and the different parsers can be cranky about the
        # allowed order of commands, options, and parameters. So we start
        # by trying to "normalize" it all.
        options = []
        params = []
        positional = []
        optarg = False
        for tok in argv:
            if optarg:
                options.append(tok)
                optarg = False
            elif tok.startswith("-"):
                options.append(tok)
                if tok != "--debug":
                    optarg = True
            elif "=" in tok:
                params.append(tok)
            else:
                positional.append(tok)

        try:
            command = positional[0]
        except IndexError:
            print("Usage: {0} command artifact ...".format(self.script))
            sys.exit(1)

        empty_artifact_commands = {'start', 'status', 'stop', 'restart',
                                   'init', 'save', 'switch', 'clear',
                                   'log', 'run', 'debug'}
        try:
            artifact = positional[1]
        except IndexError:
            if command in empty_artifact_commands:
                pass
            else:
                print("Usage: {0} command artifact ...".format(self.script))
                sys.exit(1)

        # Hack for the server case
        if artifact in ['http', 'xdbc', 'odbc', 'webdav']:
            stype = artifact
            if command == 'list':
                artifact = 'servers'
            else:
                artifact = 'server'
            positional[1] = artifact
            if positional[2] == artifact:
                del(positional[2])
            options.append("--type")
            options.append(stype)

        # Hack for the stop and restart cases
        if (command == 'stop' or command == 'restart') and artifact is None:
            positional.append('host')
            artifact = 'host'

        argv = []
        argv.extend(options)
        argv.extend(positional)
        argv.extend(params)

        templ = self.cli.command_template(command)
        if templ is None:
            print("The command '{0}' is unrecognized".format(command))
            sys.exit(1)

        if artifact is None:
            if 'parser' in templ:
                parser = templ['parser']
            else:
                print("The command '{0}' isn't recognized.".format(command))
                sys.exit(1)
        else:
            if artifact in templ:
                parser = templ[artifact]["parser"]
            else:
                print("The command '{0}' doesn't take '{1}' artifacts."
                      .format(command, artifact))
                sys.exit(1)

        args = vars(parser.parse_args(argv))

        if args['debug']:
            if args['debug'] == 'debug':
                # This is the debug command, not the debug option!
                pass
            else:
                logging.basicConfig(level=logging.WARNING)
                logging.getLogger("requests").setLevel(logging.WARNING)
                logging.getLogger("marklogic").setLevel(logging.DEBUG)
        else:
            logging.basicConfig(level=logging.INFO)
            logging.getLogger("requests").setLevel(logging.WARNING)
            logging.getLogger("marklogic").setLevel(logging.INFO)

        try:
            username, password = re.split(":", args['credentials'])
        except ValueError:
            print("--credentials value must be 'user:password':",
                  args['credentials'])
            sys.exit(1)

        if self.connection is None:
            host = args['hostname'].split(":")[0]
            try:
                mgmt_port = args['hostname'].split(":")[1]
            except IndexError:
                mgmt_port = 8002
            self.connection = Connection(host,
                                         HTTPDigestAuth(username, password),
                                         management_port=mgmt_port)

        # do it!
        if command == 'run':
            self.process_script(args['script'])
        else:
            if artifact is None:
                templ["code"](args, self.config, self.connection)
            else:
                templ[artifact]["code"](args, self.config, self.connection)

Example 40

Project: python_api Source File: __init__.py
    @classmethod
    def instance_admin(cls,host,realm,admin,password):
        """
        Initializes the security database of a newly initialized server.

        :param host: The name or IP address of the host to initialize
        :param realm: The security realm to install
        :param admin: The name of the admin user
        :param password: The password of the admin user
        """
        conn = Connection(host, None)

        payload = {
            'admin-username': admin,
            'admin-password': password,
            'realm': realm
            }

        uri = "{0}://{1}:8001/admin/v1/instance-admin".format(
            conn.protocol, conn.host)

        logger = logging.getLogger("marklogic")
        logger.debug("Initializing security for {0}".format(host))

        # N.B. Can't use conn.post here because we don't need auth yet
        response = requests.post(uri, json=payload,
                                 headers={'content-type': 'application/json',
                                          'accept': 'application/json'})

        if response.status_code != 202:
            raise UnexpectedManagementAPIResponse(response.text)

        # From now on connections require auth...
        conn = Connection(host, HTTPDigestAuth(admin, password))
        data = json.loads(response.text)
        conn.wait_for_restart(data["restart"]["last-startup"][0]["value"])

Example 41

Project: python_api Source File: mlconfig.py
Function: init
    def __init__(self, *args, **kwargs):
        super(MLConfig,self).__init__(*args, **kwargs)

        config = { "hostname": "localhost", \
                       "username": "admin", \
                       "password": "admin", \
                       "protocol": "http", \
                       "port": 8000, \
                       "management-port": 8002, \
                       "root": "manage", \
                       "version": "v2", \
                       "client-version": "v1" }
        try:
            data_file = open("mlconfig.json").read()
            data = json.loads(data_file)
            for key in data:
                config[key] = data[key]
        except FileNotFoundError:
            pass

        self.auth = HTTPDigestAuth(config["username"], config["password"])
        self.connection = Connection(config["hostname"], self.auth, \
                                         protocol=config["protocol"], \
                                         port=config["port"], \
                                         management_port=config["management-port"], \
                                         root=config["root"], \
                                         version=config["version"], \
                                         client_version=config["client-version"])

Example 42

Project: RHEAS Source File: modscag.py
def download(dbname, dts, bbox):
    """Downloads the MODSCAG snow cover fraction data product for a specific
    date *dt* and imports it into the PostGIS database *dbname*."""
    log = logging.getLogger(__name__)
    res = 0.01
    tiles = modis.findTiles(bbox)
    for dt in [dts[0] + timedelta(dti) for dti in range((dts[-1] - dts[0]).days + 1)]:
        temppath = tempfile.mkdtemp()
        url = "https://snow-data.jpl.nasa.gov/modscag-historic/{0}/{1}".format(dt.year, dt.strftime("%j"))
        r = requests.get(url, auth=HTTPDigestAuth(username, password))
        if r.status_code == 200:
            dom = lxml.html.fromstring(r.text)
            links = [link for link in dom.xpath('//a/@href') if link.find("snow_fraction.tif") > 0]
            for t in tiles:
                filenames = filter(lambda f: f.find("h{0:02d}v{1:02d}".format(t[1], t[0])) > 0, links)
                if len(filenames) > 0:
                    filename = filenames[0]
                    r = requests.get("{0}/{1}".format(url, filename), auth=HTTPDigestAuth(username, password))
                    with open("{0}/{1}".format(temppath, filename), 'wb') as fout:
                        fout.write(r.content)
            tifs = glob.glob("{0}/*.tif".format(temppath))
            if len(tifs) > 0:
                proc = subprocess.Popen(["gdal_merge.py", "-o", "{0}/snow.tif".format(temppath)] + tifs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                out, err = proc.communicate()
                log.debug(out)
                proc = subprocess.Popen(["gdal_calc.py", "-A", "{0}/snow.tif".format(temppath), "--outfile={0}/snow1.tif".format(
                    temppath), "--NoDataValue=-9999", "--calc=\"(A<101.0)*(A+9999.0)-9999.0\""], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                out, err = proc.communicate()
                log.debug(out)
                proc = subprocess.Popen(["gdalwarp", "-t_srs", "'+proj=latlong +ellps=sphere'", "-tr", str(
                    res), str(-res), "{0}/snow1.tif".format(temppath), "{0}/snow2.tif".format(temppath)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                out, err = proc.communicate()
                log.debug(out)
                if bbox is None:
                    pstr = []
                else:
                    pstr = ["-projwin", str(bbox[0]), str(bbox[3]), str(bbox[2]), str(bbox[1])]
                proc = subprocess.Popen(["gdal_translate", "-a_srs", "epsg:4326"] + pstr + ["{0}/snow2.tif".format(temppath), "{0}/snow3.tif".format(temppath)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                out, err = proc.communicate()
                log.debug(out)
                dbio.ingest(dbname, "{0}/snow3.tif".format(temppath), dt, table, False)
                shutil.rmtree(temppath)
            else:
                log.warning("MODSCAG data not available for {0}. Skipping download!".format(
                    dt.strftime("%Y-%m-%d")))

Example 43

Project: release-tools Source File: aclmanager.py
def modify_gerrit_groups(args):
    """Handles the 'groups' action"""

    gerritauth = requests.auth.HTTPDigestAuth(args.username, getpass.getpass())
    if args.stage == 'pre_release':
        # At pre-release stage we want to have $PROJECT-release and
        # Release Managers (and remove $PROJECT-stable-maint if present)
        actions = [
            ('PUT', lambda x: '%s-release' % x),
            ('PUT', lambda x: 'Release Managers'),
            ('DELETE', lambda x: '%s-stable-maint' % x),
        ]
    elif args.stage == 'post_release':
        # At post-release stage we want to have $PROJECT-stable-maint
        # (and remove Release Managers and $PROJECT-release if present)
        actions = [
            ('PUT', lambda x: '%s-stable-maint' % x),
            ('DELETE', lambda x: 'Release Managers'),
            ('DELETE', lambda x: '%s-release' % x),
        ]

    # Build the list of calls to make
    print('Computing the list of modifications')
    calls = set()
    for team, _ in repositories_list():
        group = '%s-release-branch' % team
        for (verb, memberformat) in actions:
            member = memberformat(team)
            # Filter based on already-handled names
            if gerrit_group_membership_test(gerritauth, verb, group, member):
                calls.add((verb, group, member))
            else:
                print("Skipping %s %s in %s (already done)" %
                      (verb, member, group))

    for verb, group, member in calls:
        call = 'a/groups/%s/groups/%s' % (group, member)
        print('Updating %s group using %s %s' % (group, verb, call))
        if not args.dryrun:
            r = requests.request(verb, GERRIT_URL + call, auth=gerritauth)
            if r.status_code not in (201, 204):
                print('Error (%d) while updating group' % r.status_code)

Example 44

Project: reviewstats Source File: utils.py
Function: get_team_members
def get_team_members(team_name, server, user, pw):
    global TEAM_MEMBERS
    if team_name in TEAM_MEMBERS:
        return TEAM_MEMBERS[team_name]
    auth = requests.auth.HTTPDigestAuth(user, pw)
    groups_request = requests.get('http://%s/a/groups/' % server, auth=auth)
    if groups_request.status_code != 200:
        raise Exception('Please provide your Gerrit HTTP Password.')
    text = groups_request.text
    teams = json.loads(text[text.find('{'):])
    text = requests.get('http://%s/a/groups/%s/detail' % (server,
                        teams[team_name]['id']), auth=auth).text
    team = json.loads(text[text.find('{'):])
    members_list = [n['username'] for n in team['members'] if 'username' in n]
    if 'hudson-openstack' in members_list:
        # This is a review.openstack.org specific hack.  This user is
        # automatically included in core teams, but we don't want to include it
        # in the stats.
        members_list.remove('hudson-openstack')
    TEAM_MEMBERS[team_name] = members_list
    return members_list