Exemplo n.º 1
0
    def get_request(self, url, timeout):
        """
        Process a GET requests.

        :type url: str
        :param url: URL to be sent

        :type timeout: int
        :param timeout: URL response timeout, in seconds

        :rtype: str
        :return: URL response

        """
        self.__log("HTTP GET request : " + str(url))
        try:
            response = requests.get(url=url,
                                    timeout=timeout,
                                    proxies=self.__proxies)
            return self.__process_response(response)

        except exceptions.Timeout:
            raise HTTPRequestException(HTTPRequestException.REQUEST_TIMEOUT)
        except exceptions.ConnectionError:
            raise HTTPRequestException(HTTPRequestException.CONNECTION_ERROR)
        except exceptions.MissingSchema:
            raise HTTPRequestException(HTTPRequestException.MALFORMED_REQUEST)
        except Exception as e:
            self.__log("Unknown Exception occurred during http GET request: " +
                       str(e))
            raise
Exemplo n.º 2
0
    def post_request(self, url, post_data, timeout):
        """
        Process a POST http request.

        :type url: str
        :param url: URL to be sent

        :type timeout: int
        :param timeout: URL response timeout, in seconds

        :rtype: str
        :return: URL response
        """
        self.__log("HTTP POST request : " + str(url))
        headers = {'content-type': 'text/plain'}
        try:
            response = requests.post(url,
                                     data=post_data,
                                     headers=headers,
                                     timeout=timeout,
                                     proxies=self.__proxies)
            return self.__process_response(response)
        except exceptions.Timeout:
            raise HTTPRequestException(HTTPRequestException.REQUEST_TIMEOUT)
        except exceptions.ConnectionError:
            raise HTTPRequestException(HTTPRequestException.CONNECTION_ERROR)
        except exceptions.MissingSchema:
            raise HTTPRequestException(HTTPRequestException.MALFORMED_REQUEST)
        except Exception as e:
            self.__log(
                "Unknown Exception occurred during http POST request: " +
                str(e))
            raise
Exemplo n.º 3
0
    def __on_status_permission_denied(self, response):
        """
        Process a http request response on status 550 (permission denied),
        raising an HTTPRequestException.

        :type response: requests.Response
        :param response: Response object coming from a http request

        :return: None
        """
        self.__log("HTTP request result %s: permission denied (%s)" %
                   (str(response.status_code), response.text))
        raise HTTPRequestException("HTTP status %s : permission denied" %
                                   str(response.status_code))
Exemplo n.º 4
0
    def __on_status_service_unavailable(self, response):
        """
        Process a http request response on status 503 (service not available),
        raising an HTTPRequestException.

        :type response: requests.Response
        :param response: Response object coming from a http request

        :return: None
        """
        self.__log("HTTP request result %s: service unavailable (%s)" %
                   (str(response.status_code), response.text))
        raise HTTPRequestException("HTTP status %s: service unavailable" %
                                   str(response.status_code))
Exemplo n.º 5
0
    def __on_status_bad_request(self, response):
        """
        Process a http request response on status 400 (bad request),
        raising an HTTPRequestException.

        :type response: requests.Response
        :param response: Response object coming from a http request

        :return: None
        """
        self.__log("HTTP request result %s: bad request (%s)" %
                   (str(response.status_code), response.text))
        raise HTTPRequestException("HTTP status %s: bad request" %
                                   str(response.status_code))