def server_connect(discovery, username, password):
    try:
        server = Server(discovery, user=username, password=password)
        return server
    except exceptions.ConnectionError:
        logging.info(exceptions.ConnectionError())
        return None
 def test_post_notification_connection_error(self, mock_post):
     mock_post.return_value.raise_for_status.side_effect = req_exc.ConnectionError(
         'hello')
     notification_status, notification_error = post_notification(
         None, None, None)
     self.assertEqual(notification_status, Constants.PENDING)
     self.assertEqual(notification_error, 'hello')
Example #3
0
 def test_authorize_or_401_connection_error(self, session):
     session.post.side_effect = exceptions.ConnectionError()
     response = self.client.get('/')
     assert response.status_code == 401
     assert json.loads(response.get_data(as_text=True)) == {
         "Authorized": False
     }
Example #4
0
def test_convert_requests_exception_offline():
    responses.add('GET', 'https://acme.inc/api', body=rqex.ConnectionError())
    with pytest.raises(ConnectionError):
        try:
            requests.get('https://acme.inc/api').raise_for_status()
        except rqex.ConnectionError as e:
            raise convert_requests_exception(e)
Example #5
0
 def test_max_retries_error(self, mock_request):
     mock_request.side_effect = exceptions.ConnectionError(
         urllib3.exceptions.MaxRetryError(pool="test-pool", url="test-url")
     )
     self.assertRaises(
         errors.StoreNetworkError, self.client.request, "GET", "test-url"
     )
Example #6
0
    def test_connection_error(self, requests_get: MagicMock):
        """ should fail if the url cannot be connected to """

        requests_get.side_effect = request_exceptions.ConnectionError('Fake')

        r = support.run_command('connect "a url"')
        self.assert_has_error_code(r, 'CONNECTION_ERROR')
Example #7
0
def main(title: str,
         year: str = '',
         language: str = 'english',
         resolution: str = 'all',
         limit: str = 1,
         path_to_save: str = None):
    """do some works for download subtitle"""
    downloader = Downloader()
    if not title:
        print(
            "\n'title' cannot be empty, so select movie file in window will that opens..."
        )
        movie_path = downloader.selectMovie()
        if not movie_path:
            error = 'Error:\n\tyou must be select a title for search for subtitle.\n\tyou can do that with:\n\tuse "-t" option like -> "-t <you-title-considering>"\n\tor\n\tselect movie file in window will that opens.\n\tfor this work restart program and do not pay attention to \'-t\',leave it alone.'
            print(error)
            exit(1)
        title = path.splitext(path.basename(movie_path))
    try:
        subtitleList = subtitle.search(title=title,
                                       year=year,
                                       language=language,
                                       limit=limit).ZIPlinks
    except:
        raise exceptions.ConnectionError(
            'Please check your internet connection and try again...')
    else:
        if resolution != 'all':
            subtitleList = [
                sub for sub in subtitleList if resolution in sub[0]
            ]
        downloader.downloadFile(title, subtitleList, path_to_save)
Example #8
0
    def __handle_response(self, action, url, raw=False, **kwargs):
        """Handle the response after making a HTTP request and throw any exceptions if the status is not 20x.

        :param action: The Session method to call for making the HTTP request.
        :param url: The url to make the request to.
        :param raw: Whether to return the response body as JSON or a raw data string (defaults to JSON).
        :return: The JSON dictionary from any 200 or 201 responses, or None if a 204 was encountered.
        :raise fetchcore.exceptions.RequestException: Corresponding subclass thrown for associated HTTP response error.
        """
        try:
            response = action(url, timeout=self.TIMEOUT, **kwargs)
        except requests.exceptions.RequestException as e:
            raise exceptions.ConnectionError(
                'Encountered error while making request to %s, %s', url, e)

        # 200 OKAY or 201 CREATED have a JSON response
        if response.status_code == 200 or response.status_code == 201:
            return response.content if raw else response.json()
        # 204 NO CONTENT is okay, but we have nothing to parse
        elif response.status_code == 204:
            return None
        elif response.status_code == 400:
            self.__log_response_warning(response)
            raise exceptions.BadRequest(response.text, response.url,
                                        response.status_code, response.json())
        elif response.status_code == 401:
            self.__log_response_warning(response)
            raise exceptions.Unauthorized(
                response.json().get('detail', response.text), response.url,
                response.status_code, response.json())
        elif response.status_code == 403:
            self.__log_response_warning(response)
            raise exceptions.Forbidden(
                response.json().get('detail', response.text), response.url,
                response.status_code, response.json())
        elif response.status_code == 404:
            self.__log_response_warning(response)
            raise exceptions.NotFound("%s Not Found" % response.url,
                                      response.url, response.status_code)
        elif response.status_code == 405:
            self.__log_response_warning(response)
            raise exceptions.MethodNotAllowed(
                response.json().get('detail', response.text), response.url,
                response.status_code, response.json())
        elif response.status_code == 409:
            self.__log_response_warning(response)
            # TODO: verify 409 returns a detail (we have yet to find something that returns a 409)
            raise exceptions.Conflict(
                response.json().get('detail', response.text), response.url,
                response.status_code, response.json())
        elif response.status_code == 500:
            self.__log_response_error(response)

            raise exceptions.InternalServerError(response.text, response.url,
                                                 response.status_code)
        else:
            # TODO add other 400 errors if Django even throws them
            raise exceptions.RequestException(response.text, response.url,
                                              response.status_code)
Example #9
0
    def test__exec_cmd_http_get_fail(self, mock_get):
        fake_rel_url = 'fake_rel_url'
        mock_get.side_effect = iter([requests_exceptions.ConnectionError('x')])

        self.assertRaises(exception.MSFTOCSClientApiException,
                          self._client._exec_cmd, fake_rel_url)
        mock_get.assert_called_once_with(self._fake_base_url + "/" +
                                         fake_rel_url,
                                         auth=mock.ANY)
Example #10
0
 def get_id(self):
     # Authenticate
     endpoint = "auth"
     r = self.s_recv.get(self.url.format(endpoint))
     #print(r)
     if (r.status_code == 200):
         myid = r.json()["userid"]
         return myid
     else:
         raise exceptions.ConnectionError("Server is down :(")
    def get_torrents(self):
        # Needs User-Agent header otherwise it throws 403
        headers = {'User-Agent': TorrentsUrlProvider.USER_AGENT}
        response = get(self.url, headers=headers)

        if response.ok:
            return response.content
        else:
            raise exceptions.ConnectionError(
                f'Unable to reach tracker. Response code: {response.status_code}.'
            )
Example #12
0
    def test_v2_authenticate_connection_error(self, get_mock, post_mock):
        get_mock.return_value = self._valid_get_response()
        post_mock.side_effect = [
            self._valid_post_response(),
            req_ex.ConnectionError('ConnectionError')
        ]

        identity = v2.RackspaceIdentity.from_username(
            self.username, self.password, user_domain_id=self.domain_id)

        self.assertRaises(exception.BadGateway, identity.authenticate)
Example #13
0
 def setUp(self):
     super(TestHangupExceptionErrorHandler, self).setUp()
     self.handler = _HangupExceptionHandler(self.connection, s.user_uuid)
     self.exception_msg_map = {
         exceptions.HTTPError(response=Mock(status_code=401)):
         'hangup_unauthorized',
         exceptions.ConnectionError():
         'service_unavailable',
         exceptions.HTTPError(response=Mock(status_code=503)):
         'service_unavailable',
     }
Example #14
0
def callback_requests_get(url):
    url_mapping = {
        URL_GITHUB_API: GITHUB_API_RESPONSE_FILEPATH,
        URL_GITHUB_DOCS: GITHUB_DOCS_RESPONSE_FILEPATH,
    }
    if url in url_mapping:
        mock_content_filepath = url_mapping[url]
        with open(mock_content_filepath, "rb") as content_fin:
            content = content_fin.read()
        mock_response = build_response_mock_from_content(content=content)
        return mock_response
    raise exceptions.ConnectionError(f"URL not Found {url}")
Example #15
0
def test_request_any_other_exception():
  session = create_autospec(spec=requests.Session, instance=True)
  session.headers = {}
  session.post = Mock(side_effect=request_exceptions.ConnectionError())
  transport = TRequestsTransport('http://localhost:12345', session_factory=lambda: session)
  protocol = TJSONProtocol.TJSONProtocol(transport)
  client = ReadOnlyScheduler.Client(protocol)

  with pytest.raises(TTransport.TTransportException):
    client.getRoleSummary()

  transport.close()
Example #16
0
    def connect(self, timeout=DEFAULT_TIMEOUT):
        """Connect and authenticate to the server.

        :param timeout: The timeout (in seconds) for the connection.
        :raise: fetchcore.exceptions.ConnectionError Thrown if there is a problem creating communicating with fetchcore.
        """
        # TODO: use timeout in request
        if self.is_connected:
            # Don't connect twice
            return

        # The authentication token for all requests
        try:
            token = self.get_authentication_token(timeout)
        except exceptions.ConnectionError as err:
            if self.handle_reconnect:
                return
            else:
                logging.warn("Failed to get authentication token from server.")
                raise err

        # Retrieve IP (original code: http://stackoverflow.com/a/166589)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            s.connect((self._Client__host, self._Client__port))
            self.ip = s.getsockname()[0]
        except socket.gaierror:
            raise exceptions.ConnectionError(
                "Failed to connect to %s:%s when retrieving IP." %
                (self._Client__host, self._Client__port))
        except Exception as err:
            raise exceptions.ConnectionError(err.message)
        finally:
            s.close()

        # Parse out the token
        self.__session.headers.update({'AUTHORIZATION': 'Token ' + token})
        self._connected = True
        if self.on_connect:
            self.on_connect()
Example #17
0
def callback_requests_get(url):
    url_mapping = {
        URL_CBR_DAILY: CBR_DAILY_RESPONSE_FILEPATH,
        URL_CBR_INDICATORS: CBR_INDICATORS_RESPONSE_FILEPATH,
    }
    if url in url_mapping:
        mock_content_filepath = url_mapping[url]
        with open(mock_content_filepath, "rb") as content_fin:
            content = content_fin.read()
        mock_response = build_response_mock_from_content(content=content)
        return mock_response

    raise exceptions.ConnectionError(f"Exceeded max trial connection to {url}")
Example #18
0
def get(session, url):
    response = None
    i = 3
    while i > 0:
        try:
            with session.get(url) as response:
                return response.text
        except req_exc.ConnectionError:
            i -= 1
            sleep(2)
    if not i > 0:
        raise req_exc.ConnectionError(f'Too many attempts to url {url}')
    return response
Example #19
0
 def setUp(self):
     super(TestTransferExceptionErrorHandler, self).setUp()
     self.handler = _TransferExceptionHandler(self.connection, s.user_id,
                                              '1234')
     self.exception_msg_map = {
         exceptions.HTTPError(response=Mock(status_code=401)):
         'transfer_unauthorized',
         exceptions.ConnectionError():
         'service_unavailable',
         exceptions.HTTPError(response=Mock(status_code=503)):
         'service_unavailable',
         exceptions.HTTPError(response=Mock(status_code=400)):
         'unreachable_extension:1234'
     }
Example #20
0
def test_raises_networkfailure_on_connectionerror(mock_command):
    "If an error occurs downloading the ZIP file, and error is raised."
    mock_command.download_url.side_effect = requests_exceptions.ConnectionError()

    with pytest.raises(NetworkFailure):
        AndroidSDK.verify(mock_command, jdk=MagicMock())

    # The download was attempted
    mock_command.download_url.assert_called_once_with(
        url="https://dl.google.com/android/repository/sdk-tools-unknown-4333796.zip",
        download_path=mock_command.tools_path,
    )
    # But no unpack occurred
    assert mock_command.shutil.unpack_archive.call_count == 0
Example #21
0
    def delete(self, endpoint, pk):
        """Make a DELETE request to the given endpoint. Throws an appropriate exception on failure. Note that nothing is
        returned as a successful delete request is a 204 No Content.

        :param endpoint: The endpoint to call (e.g., actions/definitions).
        :param pk: The primary key of the entity to delete.
        :raise fetchcore.exceptions.ConnectionError: Thrown if a call is made without being connected to fetchcore.
        :raise fetchcore.exceptions.RequestException: Corresponding subclass thrown for associated HTTP response error.
        """
        # Check if we are connected
        if not self.is_connected:
            raise exceptions.ConnectionError(
                "Client disconnected, request cannot be made.")
        # Will throw the exception if an error is detected
        self.__handle_response(self.__session.delete,
                               self.__build_api_url(endpoint, pk))
Example #22
0
 def obtener_url_foto_de_tweet(self, tweet):
     url_imagen = None
     try:
         if self.tiene_foto(tweet):
             url_imagen = tweet['entities']['media'][0]['media_url']  #twimg
         elif self.tiene_foto_externa(tweet):
             url_completa = tweet['entities']['urls'][0][
                 'expanded_url']  #instagram
             url_imagen = self.sacar_link_a_foto_instagram(url_completa)
         return url_imagen
     except exceptions.ConnectionError as conerr:
         raise exceptions.ConnectionError(
             u'No se ha podido conectar para obtener la url de la foto. {0}'
             .format(conerr.message))
     except HTMLParser.HTMLParseError as prserr:
         raise HTMLParser.HTMLParseError(prserr.message)
Example #23
0
    def post(self, endpoint, data, files=None):
        """Make a POST request to the given endpoint. Throws an appropriate exception on failure.

        :param endpoint: The endpoint to call (e.g., actions/definitions).
        :param data: The data to send with the request as a JSON dictionary.
        :param files: Optional dictionary of objects to send as a multipart-encoded request.
        :return: The JSON response from fetchcore.
        :raise fetchcore.exceptions.ConnectionError: Thrown if a call is made without being connected to fetchcore.
        :raise fetchcore.exceptions.RequestException: Corresponding subclass thrown for associated HTTP response error.
        """
        # Check if we are connected
        if not self.is_connected:
            raise exceptions.ConnectionError(
                "Client disconnected, request cannot be made.")
        kwargs = {'data': data, 'files': files} if files else {'json': data}
        return self.__handle_response(self.__session.post,
                                      self.__build_api_url(endpoint), **kwargs)
 def test_analyze_exception_request(self):
     self.assertEqual(errors.SYSTEM_TRANSIENT,
                      self.apic_handler.analyze_exception(rexc.Timeout()))
     self.assertEqual(
         errors.SYSTEM_TRANSIENT,
         self.apic_handler.analyze_exception(rexc.ConnectionError()))
     self.assertEqual(
         errors.OPERATION_CRITICAL,
         self.apic_handler.analyze_exception(rexc.InvalidURL()))
     self.assertEqual(
         errors.OPERATION_CRITICAL,
         self.apic_handler.analyze_exception(rexc.URLRequired()))
     self.assertEqual(
         errors.OPERATION_TRANSIENT,
         self.apic_handler.analyze_exception(rexc.RequestException()))
     self.assertEqual(errors.UNKNOWN,
                      self.apic_handler.analyze_exception(Exception()))
Example #25
0
 def __init__(self, city_name):
     self.name = str(city_name).capitalize()
     try:
         self._request = requests.get(City._base_url.format(self.name))
     except e:
         raise exceptions.ConnectionError(e)
     if self._request.status_code != 200:
         raise exceptions.RequestException(self._request.status_code, self._request.text)
     if len(self._request.json()) == 0:
         raise NameError('Unknown city name: {}'.format(city_name))
     else:
         try:
             station = next(match for match in self._request.json() if match["category"] == "station")
         except StopIteration as e:
             raise NameError('Not train station found for {}.\nBest result was {}'.format(self.name, self._request.json()[0]))
         self.code = station['id']
         self.label = station['label']
Example #26
0
def test_request_any_other_exception():
    session = mock.MagicMock(spec=requests.Session)
    session.headers = {}
    session.post = mock.Mock(side_effect=request_exceptions.ConnectionError())
    transport = TRequestsTransport('http://localhost:12345',
                                   session_factory=lambda: session)
    protocol = TJSONProtocol.TJSONProtocol(transport)
    client = ReadOnlyScheduler.Client(protocol)

    try:
        client.getRoleSummary()
        assert False, 'getRoleSummary should not succeed'
    except TTransport.TTransportException:
        pass
    except Exception as e:
        assert False, 'Only expected TTransportException, got %s' % e

    transport.close()
Example #27
0
def main():
    # logging to file
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    # logging to file
    # formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
    # file_handler = RotatingFileHandler('/var/log/surfcheck/openweather_data.log', 'a', 1000000, 1)
    # file_handler.setLevel(logging.INFO)
    # file_handler.setFormatter(formatter)
    # logger.addHandler(file_handler)

    # loggging to console
    steam_handler = logging.StreamHandler()
    steam_handler.setLevel(logging.DEBUG)
    logger.addHandler(steam_handler)

    # db access
    client = MongoClient(os.environ['MONGODB_URI'],
                         connectTimeoutMS=30000,
                         socketTimeoutMS=None,
                         socketKeepAlive=True)
    db = client.get_default_database()
    weather_data = db.weather_data

    # openweather parameters
    api_url = "http://api.openweathermap.org/data/2.5/weather?"
    location = "Biarritz,fr"
    appid = "e30f8cfd339545060d2d87d93a8f1afc"
    url = api_url + "q=" + location + "&appid=" + appid

    logger.debug("Connecting to openweathermap.org")
    r = requests.get(url)
    if r.status_code != 200:
        logger.error("error retrieving data with request " + url +
                     " (status code : " + r.status_code + ")")
        raise exceptions.ConnectionError(r.text)

    logger.debug("Query succeeded (" + str(r.status_code) + ")")
    data = r.json()
    data["datetime"] = datetime.today()
    result = weather_data.insert(data)
    logger.info("Data inserted for " + location + " for datetime: " +
                str(data["datetime"]))
Example #28
0
    def test_exception(self):
        """Test retrying."""
        self.http.post.side_effect = ([exceptions.ConnectionError()] *
                                      (common.RETRY_COUNT + 1))

        with self.assertRaises(exceptions.ConnectionError):
            common.post(url='a',
                        headers={'c': 'd'},
                        data={'e': 'f'},
                        random='thing')

        self.assertTrue(os.path.exists(common.CLUSTERFUZZ_TESTCASES_DIR))
        self.assertEqual(common.RETRY_COUNT + 1,
                         self.mock.CachedSession.call_count)
        self.assertEqual(common.RETRY_COUNT + 1, self.http.mount.call_count)
        self.assert_exact_calls(self.http.post, [
            mock.call(
                url='a', headers={'c': 'd'}, data={'e': 'f'}, random='thing')
        ] * (common.RETRY_COUNT + 1))
Example #29
0
def get_running_containers(project_name: str) -> tuple:
    """Get the number of running containers and theirs details for the current stakkr instance."""
    from requests import exceptions

    filters = {
        'name': '{}_'.format(project_name),
        'status': 'running',
        'network': '{}_stakkr'.format(project_name).lower()
    }

    try:
        cts = get_client().containers.list(filters=filters)
    except exceptions.ConnectionError:
        raise exceptions.ConnectionError(
            'Make sure docker is installed and running')

    __st__['cts_info'] = dict()
    for container in cts:
        container_info = _extract_container_info(project_name, container.id)
        __st__['cts_info'][container_info['name']] = container_info

    __st__['running_cts'] = len(cts)

    return __st__['running_cts'], __st__['cts_info']
Example #30
0
    def check_url(self):
        try:

            self.r.raise_for_status()
            print('Successful connection!', self.url)
        except exceptions.HTTPError:
            raise exceptions.HTTPError(
                'Something went wrong.Incorrect URL or check your internet connection!'
                '\nCheck if the address is correct')

        except exceptions.ConnectionError:
            raise exceptions.ConnectionError(
                f"Something went wrong. Server Error!\nCan't find {self.url}!"
                f"\nCheck if the address is correct")
        except exceptions.MissingSchema:
            raise exceptions.MissingSchema(
                f"Something went wrong. Invalid url!\nTry https://{self.url}!"
                f"\n'Check if the address is correct'")

        except exceptions.InvalidSchema:
            raise exceptions.InvalidSchema(
                f"Something went wrong. Invalid url {self.url}!"
                f"\nCheck if the address is correct")
        return self.url