Python httplib.CannotSendRequest() Examples

The following are 9 code examples of httplib.CannotSendRequest(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib , or try the search function .
Example #1
Source File: connection.py    From neo4jdb-python with MIT License 6 votes vote down vote up
def _http_req(self, method, path, payload=None, retries=2):
        serialized_payload = json.dumps(payload) if payload is not None else None

        try:
            self._http.request(method, path, serialized_payload, self._COMMON_HEADERS)
            http_response = self._http.getresponse()
        except (http.BadStatusLine, http.CannotSendRequest):
            self._http = http.HTTPConnection(self._host)
            if retries > 0:
                return self._http_req(method, path, payload, retries-1)
            self._handle_error(self, None, Connection.OperationalError, "Connection has expired.")

        if not http_response.status in [200, 201]:
            message = "Server returned unexpected response: " + ustr(http_response.status) + ustr(http_response.read())
            self._handle_error(self, None, Connection.OperationalError, message)

        return http_response 
Example #2
Source File: foctor_core.py    From centinel with MIT License 6 votes vote down vote up
def save_html(driver, filename, path):
    make_folder(path)
    html_path = path + str(filename) + ".html"
    f = open(html_path, "w")
    done = False
    while done is False:
        try:
            f.write(driver.page_source.encode("UTF-8"))
            done = True
        except CannotSendRequest:
            done = False
            time.sleep(1)
        except (TimeoutError, TimeoutException):
            logging.warning("save_html() timed out")
            return "Timed-out"
    f.close()
    return "No Error" 
Example #3
Source File: base.py    From iugu-python with Apache License 2.0 6 votes vote down vote up
def __conn_request(self, http_verb, urn, params):
        """
        Wrapper to request/response of httplib's context, reload a
        connection if presume that error will occurs and returns the response
        """
        try:
            self.__conn.request(http_verb, urn, params, self.headers)
        except CannotSendRequest:
            self.__reload_conn()
            self.__conn.request(http_verb, urn, params, self.headers)

        try:
            response = self.__conn.getresponse()
        except (IOError, BadStatusLine):
            self.__reload_conn()
            self.__conn.request(http_verb, urn, params, self.headers)
            response = self.__conn.getresponse()

        return response 
Example #4
Source File: foctor_core.py    From centinel with MIT License 5 votes vote down vote up
def save_screenshot(driver, filename, path):
    make_folder(path)
    ss_path = path + str(filename) + ".png"
    done = False
    while done is False:
        try:
            driver.save_screenshot(ss_path)
            done = True
        except CannotSendRequest:
            done = False
            time.sleep(1)
        except (TimeoutError, TimeoutException):
            logging.warning("save_screenshot() timed out")
            return "Timed-out"
    return "No Error" 
Example #5
Source File: foctor_core.py    From centinel with MIT License 5 votes vote down vote up
def abort_load(driver, display, tor, tor_call, display_mode, capture_path, process_tag, port, exits):
    logging.debug("Aborting page load...")
    not_closed = True
    while not_closed:
        try:
            driver, display, tor_call = restart_driver(driver, display, tor, tor_call, display_mode, capture_path,
                                                       port, process_tag, exits)
            not_closed = False
            time.sleep(10)
        except CannotSendRequest:
            not_closed = True
    return driver, display 
Example #6
Source File: base.py    From iugu-python with Apache License 2.0 5 votes vote down vote up
def __reload_conn(self):
        """
        Wrapper to keep TCP connection ESTABLISHED. Rather the connection go to
        CLOSE_WAIT and raise errors CannotSendRequest or the server reply with
        empty and it raise BadStatusLine
        """
        self.__conn = HTTPSConnection(config.API_HOSTNAME) # reload
        self.__conn.timeout = 10 
Example #7
Source File: __init__.py    From magnitude with MIT License 4 votes vote down vote up
def _prefetch_in_background(self, n, amount, offset):
            headers = {
                'Range': "bytes=" + str(max(offset, 0)) + "-" + str(
                    min((offset + amount) - 1, self.length)  # noqa
                ),
            }

            self._wait_on_prefetch_connection(n)
            while not self.pconn_terminated[n]:
                try:
                    self.pconn[n].request(
                        "GET", self.parsed_url.path, headers=headers)
                    break
                except CannotSendRequest:
                    sleep(1)
            while not self.pconn_terminated[n]:
                try:
                    res = self.pconn[n].getresponse()
                    break
                except ResponseNotReady:
                    # Since we are sharing the connection wait for this to be
                    # ready
                    sleep(1)
            if self.pconn_terminated[n]:
                self._unwait_on_prefetch_connection(n)
                return
            else:
                self._unwait_on_prefetch_connection(n)

            if not(res.status >= 200 and res.status <= 299):
                # Check for a valid status from the server
                return
            data = bytearray(res.length)
            i = 0
            for piece in iter(lambda: res.read(1024), bytes('')):
                if not getattr(threading.currentThread(), "do_run", True):
                    break
                data[i:i + len(piece)] = piece
                i = i + len(piece)
            else:
                return bytes(data)

            # Leaving the thread early, without
            # reading all of the data this will
            # make the connection unusable, refresh it
            self._prepare_prefetch_connection(n) 
Example #8
Source File: __init__.py    From supersqlite with MIT License 4 votes vote down vote up
def _prefetch_in_background(self, n, amount, offset):
            headers = {
                'Range': "bytes=" + str(max(offset, 0)) + "-" + str(
                    min((offset + amount) - 1, self.length)  # noqa
                ),
            }

            self._wait_on_prefetch_connection(n)
            while not self.pconn_terminated[n]:
                try:
                    self.pconn[n].request(
                        "GET", self.parsed_url.path, headers=headers)
                    break
                except CannotSendRequest:
                    sleep(1)
            while not self.pconn_terminated[n]:
                try:
                    res = self.pconn[n].getresponse()
                    break
                except ResponseNotReady:
                    # Since we are sharing the connection wait for this to be
                    # ready
                    sleep(1)
            if self.pconn_terminated[n]:
                self._unwait_on_prefetch_connection(n)
                return
            else:
                self._unwait_on_prefetch_connection(n)

            if not(res.status >= 200 and res.status <= 299):
                # Check for a valid status from the server
                return
            data = bytearray(res.length)
            i = 0
            for piece in iter(lambda: res.read(1024), bytes('')):
                if not getattr(threading.currentThread(), "do_run", True):
                    break
                data[i:i + len(piece)] = piece
                i = i + len(piece)
            else:
                return bytes(data)

            # Leaving the thread early, without
            # reading all of the data this will
            # make the connection unusable, refresh it
            self._prepare_prefetch_connection(n) 
Example #9
Source File: foctor_core.py    From centinel with MIT License 4 votes vote down vote up
def load_page(driver, url, cookies=0):
    try:
        if cookies == 0:
            driver.delete_all_cookies()
            time.sleep(1)
        if "http" not in url.split("/")[0]:
            url = "http://" + url

        switch_tab(driver)
        driver.get(url)

        logging.debug("driver.get(%s) returned successfully" % url)
    except (TimeoutException, TimeoutError) as te:
        logging.warning("Loading %s timed out" % url)
        return str(te)
    # try:
    #     element = WebDriverWait(driver, .5).until(EC.alert_is_present())
    #     if element is not None:
    #         print "Alert found on page: " + url
    #         sys.stdout.flush()
    #         raise TimeoutError
    #     else:
    #         raise NoAlertPresentException
    # except (TimeoutException, NoAlertPresentException):
    #     print "No alert found on page: " + url
    #     sys.stdout.flush()
    #     pass
    # except TimeoutError as te:
    #     sys.stdout.flush()
    #     return str(te)
    # try:
    #     main_handle = driver.current_window_handle
    # except CannotSendRequest as csr:
    #     return str(csr)
    try:
        windows = driver.window_handles
        if len(windows) > 1:
            logging.debug("Pop up detected on page: %s. Closing driver instance." % url)
            raise TimeoutError
            # for window in windows:
            #     if window != main_handle:
            #         driver.switch_to_window(window)
            #         driver.close()
            # driver.switch_to_window(main_handle)
        # wfrs_status = wait_for_ready_state(driver, 15, 'complete')
        # if wfrs_status == "Timed-out":
        #     print "wait_for_ready_state() timed out."
        #     raise TimeoutError
    except (TimeoutException, TimeoutError) as te:
        logging.warning("Loading %s timed out" % url)
        return str(te)
    return "No Error"