Python requests.packages.urllib3.exceptions.ReadTimeoutError() Examples

The following are 5 code examples of requests.packages.urllib3.exceptions.ReadTimeoutError(). 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 requests.packages.urllib3.exceptions , or try the search function .
Example #1
Source File: ABuNetWork.py    From abu with GNU General Public License v3.0 6 votes vote down vote up
def get(url, params=None, headers=None, retry=3, **kwargs):
    """
    :param url: 请求base url
    :param params: url params参数
    :param headers: http head头信息
    :param retry: 重试次数,默认retry=3
    :param kwargs: 透传给requests.get,可设置ua等,超时等参数
    """
    req_count = 0
    while req_count < retry:
        # 重试retry次
        try:
            resp = requests.get(url=url, params=params, headers=headers, **kwargs)
            if resp.status_code == 200 or resp.status_code == 206:
                # 如果200,206返回,否则继续走重试
                return resp
        except ReadTimeoutError:
            # 超时直接重试就行,不打日志
            pass
        except Exception as e:
            logging.exception(e)
        req_count += 1
        time.sleep(0.5)
        continue
    return None 
Example #2
Source File: crawler.py    From ITWSV with MIT License 5 votes vote down vote up
def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
        try:
            ret = super().send(request, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
        except ConnectionError as error:
            if hasattr(error.args[0], "reason") and isinstance(error.args[0].reason, ReadTimeoutError):
                raise ReadTimeout(error.args[0], request=request)
            raise error
        else:
            return ret 
Example #3
Source File: errors.py    From deepWordBug with Apache License 2.0 5 votes vote down vote up
def handle_connection_errors(client):
    try:
        yield
    except SSLError as e:
        log.error('SSL error: %s' % e)
        raise ConnectionError()
    except RequestsConnectionError as e:
        if e.args and isinstance(e.args[0], ReadTimeoutError):
            log_timeout_error(client.timeout)
            raise ConnectionError()
        exit_with_error(get_conn_error_message(client.base_url))
    except APIError as e:
        log_api_error(e, client.api_version)
        raise ConnectionError()
    except (ReadTimeout, socket.timeout):
        log_timeout_error(client.timeout)
        raise ConnectionError()
    except Exception as e:
        if is_windows():
            import pywintypes
            if isinstance(e, pywintypes.error):
                log_windows_pipe_error(e)
                raise ConnectionError()
        raise 
Example #4
Source File: streaming.py    From Stocktalk with MIT License 5 votes vote down vote up
def streamer(credentials, queries, refresh, sentiment=False, debug=False):
    keywords = [i for j in queries.values() for i in j]

    # User Error Checks
    if len(queries)  <= 0:  print("Error: You must include at least one query."); return
    if len(queries)  >= 10: print("Warning: Fewer than ten query recommended.")
    if len(keywords) <= 0:  print("Error: You must include at least one keyword."); return
    if len(keywords) >= 20: print("Warning: Fewer than twenty keywords recommended.")
    if refresh       <= 0:  print("Error: Refresh rate must be greater than 0"); return

    auth = tweepy.OAuthHandler(credentials[0], credentials[1])
    auth.set_access_token(credentials[2], credentials[3])

    if sentiment:
        global SentimentIntensityAnalyzer
        from nltk.sentiment.vader import SentimentIntensityAnalyzer

    while True:

        # Start streaming -----------------------------
        try:
            print("Streaming Now...")
            listener = Listener(auth, queries, refresh, sentiment, debug)
            stream = tweepy.Stream(auth, listener)
            stream.filter(track=keywords)

        except (Timeout, ConnectionError, ReadTimeoutError):
            print("{0} Error: Connection Dropped\n".format(time.strftime('%m/%d/%Y %H:%M:%S')))
            print("Re-establishing Connection...")

        time.sleep((15*60)+1) # Wait at least 15 minutes before restarting listener

        # --------------------------------------------- 
Example #5
Source File: views.py    From dissemin with GNU Affero General Public License v3.0 4 votes vote down vote up
def handleUrlDownload(request):
    response = {'status': 'error'}
    form = UrlDownloadForm(request.POST)
    if not form.is_valid():
        response['message'] = _('Invalid form.')
        return response, 400
    content = None
    try:
        r = requests.get(form.cleaned_data[
                         'url'], timeout=settings.URL_DEPOSIT_DOWNLOAD_TIMEOUT, stream=True)
        r.raise_for_status()
        content = r.raw.read(settings.DEPOSIT_MAX_FILE_SIZE+1, decode_content=False)

        if len(content) > settings.DEPOSIT_MAX_FILE_SIZE:
            response['message'] = _('File too large.')

        content_type = r.headers.get('content-type')
        if 'text/html' in content_type:
            response['message'] = (  # Left as one line for compatibility purposes
                _('Invalid content type: this link points to a web page, we need a direct link to a PDF file.'))

    except requests.exceptions.SSLError:
        response['message'] = _('Invalid SSL certificate on the remote server.')
    except requests.exceptions.Timeout:
        response['message'] = _('Invalid URL (server timed out).')
    except requests.exceptions.RequestException:
        response['message'] = _('Invalid URL.')
    except ReadTimeoutError:
        response['message'] = _('Invalid URL (server timed out).')
    except HTTPError:
        response['message'] = _('Invalid URL.')

    if 'message' in response:
        return response, 403

    orig_name = form.cleaned_data['url']

    response = save_pdf(request.user, orig_name, content)

    if response['status'] == 'error':
        return response, 403

    return response