Gather rate limit information from HTTP headers.

        The mapping providing the headers is expected to support lowercase
        keys.  Returns ``None`` if ratelimit info is not found in the headers.
960902
def from_http(cls, headers: Mapping[str, str]) -> Optional["RateLimit"]:
        
        try:
            limit = int(headers["x-ratelimit-limit"])
            remaining = int(headers["x-ratelimit-remaining"])
            reset_epoch = float(headers["x-ratelimit-reset"])
        except KeyError:
            return None
        else:
            return cls(limit=limit, remaining=remaining, reset_epoch=reset_epoch)
613618
def validate_gaslimit(self, header: BlockHeader) -> None:
        
        parent_header = self.get_block_header_by_hash(header.parent_hash)
        low_bound, high_bound = compute_gas_limit_bounds(parent_header)
        if header.gas_limit < low_bound:
            raise ValidationError(
                "The gas limit on block {0} is too low: {1}. It must be at least {2}".format(
                    encode_hex(header.hash), header.gas_limit, low_bound))
        elif header.gas_limit > high_bound:
            raise ValidationError(
                "The gas limit on block {0} is too high: {1}. It must be at most {2}".format(
                    encode_hex(header.hash), header.gas_limit, high_bound))
774997
def _process_server_headers(
        self, key: Union[str, bytes], headers: httputil.HTTPHeaders
    ) -> None:
        
        assert headers["Upgrade"].lower() == "websocket"
        assert headers["Connection"].lower() == "upgrade"
        accept = self.compute_accept_value(key)
        assert headers["Sec-Websocket-Accept"] == accept

        extensions = self._parse_extensions_header(headers)
        for ext in extensions:
            if ext[0] == "permessage-deflate" and self._compression_options is not None:
                self._create_compressors("client", ext[1])
            else:
                raise ValueError("unsupported extension %r", ext)

        self.selected_subprotocol = headers.get("Sec-WebSocket-Protocol", None)
785303
def _get_max_page(self, url):
        
        html = requests.get(url).text
        pq = PyQuery(html)
        try:
            tds = int(pq("h2").text().split()[-1])
            if tds % 25:
                return tds / 25 + 1
            return tds / 25
        except ValueError:
            raise ValueError("No results found!")
787352
def get_resp(url):
    """"""
    try:
        headers = {'User-Agent': random.choice(USER_AGENTS)}
        try:
            request = requests.get(url, headers=headers, proxies=get_proxies())
        except MissingSchema:
            url = add_protocol(url)
            request = requests.get(url, headers=headers, proxies=get_proxies())
        return lh.fromstring(request.text.encode('utf-8') if PY2 else request.text)
    except Exception:
        sys.stderr.write('Failed to retrieve {0}.\n'.format(url))
        raise
654949
def rate_limit(self, rate_limit):
        
        self._rate_limit = bool(rate_limit)
        self._rate_limit_last_call = None
        self.clear_memoized()
82840
public function getRateLimit()
    {
        if (!$this->lastResponse) {
            return null;
        }

        /** @var Header $limit */
        $limit = $this->lastResponse->getHeader('X-Rate-Limit-Limit');

        return $limit ? $limit->normalize() : null;
    }
983575
private static Iterable<HTTPHeader> getHeaders(HTTPResponse resp, String headerName) {
    final String lowercaseHeaderName = headerName.toLowerCase();
    return Iterables.filter(resp.getHeadersUncombined(), new Predicate<HTTPHeader>() {
      @Override public boolean apply(HTTPHeader header) {
        return header.getName().toLowerCase().equals(lowercaseHeaderName);
      }
    });
  }
286581
def rate_limit_info():
    
    import json
    import time

    r = requests.get(gh_url + "/rate_limit", auth=login.auth())
    out = json.loads(r.text)
    mins = (out["resources"]["core"]["reset"]-time.time())/60
    return out["resources"]["core"]["remaining"], mins
88817
private boolean isRateLimitReached(ResponseEntity<String> response) {
        HttpHeaders header = response.getHeaders();
        List<String> limit = header.get("X-RateLimit-Remaining");
        boolean rateLimitReached =  CollectionUtils.isEmpty(limit) ? false : Integer.valueOf(limit.get(0)) < settings.getRateLimitThreshold();
        if (rateLimitReached) {
            LOG.error("Github rate limit reached. Threshold =" + settings.getRateLimitThreshold() + ". Current remaining ="+Integer.valueOf(limit.get(0)));
        }
        return rateLimitReached;
    }
1279072
def http_range(self) -> slice:
        
        rng = self._headers.get(hdrs.RANGE)
        start, end = None, None
        if rng is not None:
            try:
                pattern = r'^bytes=(\d*)-(\d*)$'
                start, end = re.findall(pattern, rng)[0]
            except IndexError:  # pattern was not found in header
                raise ValueError("range not in acceptable format")

            end = int(end) if end else None
            start = int(start) if start else None

            if start is None and end is not None:
                # end with no start is to return tail of content
                start = -end
                end = None

            if start is not None and end is not None:
                # end is inclusive in range header, exclusive for slice
                end += 1

                if start >= end:
                    raise ValueError('start cannot be after end')

            if start is end is None:  # No valid range supplied
                raise ValueError('No start or end of range specified')

        return slice(start, end, 1)
715983
protected void checkLimits() throws RecorderIOException {
        // too much material before finding end of headers? 
        if (messageBodyBeginMark<0) {
            // no mark yet
            if(position>MAX_HEADER_MATERIAL) {
                throw new RecorderTooMuchHeaderException();
            }
        }
        // overlong?
        if(position>maxLength) {
            throw new RecorderLengthExceededException(); 
        }
        // taking too long? 
        long duration = System.currentTimeMillis() - startTime; 
        duration = Math.max(duration,1); // !divzero
        if(duration>timeoutMs) {
            throw new RecorderTimeoutException(); 
        }
        // need to throttle reading to hit max configured rate? 
        if(position/duration >= maxRateBytesPerMs) {
            long desiredDuration = position / maxRateBytesPerMs;
            try {
                Thread.sleep(desiredDuration-duration);
            } catch (InterruptedException e) {
                logger.log(Level.WARNING,
                        "bandwidth throttling sleep interrupted", e);
            } 
        }
    }
125018
def get_limit(self, request):
        """"""
        if self.limit_query_param:
            try:
                return _positive_int(
                    get_query_param(request, self.limit_query_param),
                    strict=True,
                    cutoff=self.max_limit
                )
            except (KeyError, ValueError):
                pass

        return self.default_limit
362614
def rate_limited(max_per_hour: int, *args: Any) -> Callable[..., Any]:
    """"""
    return util.rate_limited(max_per_hour, *args)
543333
private function rateLimit()
    {
        return [
            'code'      => $this->response['code'],
            'errors'    => $this->response['response'],
            'tts'       => isset($this->response['headers']['x-rate-limit-reset']) ? // Time To Sleep
                            ($this->response['headers']['x-rate-limit-reset'] - time()) :
                            false,
        ];
    }
293015
def open_url(self, url, headers):
        
        
        data = self.cache.get_cached_data(url)
        if data is not None:
            return data, 200, headers
        
        self.rate_limit_ensembl_requests()
        req = request.Request(url, headers=headers)
        
        try:
            handler = request.urlopen(req)
        except HTTPError as error:
            # if we get a http error, we still process the status code, since a
            # later step deals with different status codes differently.
            handler = error
        except (URLError, ConnectionResetError, TimeoutError):
            # if we get a ConnectionResetError, assume something has gone wrong
            # with the server. Later code will wait before retrying.
            return '', 500, headers
        
        status_code = handler.getcode()
        response = handler.read()
        if IS_PYTHON3:
            response = response.decode("utf-8")
        
        # parse the headers into a key, value dictionary
        headers = dict(zip(map(str.lower, handler.headers.keys()), handler.headers.values()))
        
        now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
        logging.warning("{}\t{}\t{}".format(now, status_code, url))
        
        return response, status_code, headers
782573
def get_rate_limits(response):
    """"""
    periods = response.headers['X-RateLimit-Period']
    if not periods:
        return []

    rate_limits = []

    periods = periods.split(',')
    limits = response.headers['X-RateLimit-Limit'].split(',')
    remaining = response.headers['X-RateLimit-Remaining'].split(',')
    reset = response.headers['X-RateLimit-Reset'].split(',')

    for idx, period in enumerate(periods):
        rate_limit = {}
        limit_period = get_readable_time_string(period)
        rate_limit["period"] = limit_period
        rate_limit["period_seconds"] = period
        rate_limit["request_limit"] = limits[idx]
        rate_limit["requests_remaining"] = remaining[idx]

        reset_datetime = get_datetime_from_timestamp(reset[idx])
        rate_limit["reset"] = reset_datetime

        right_now = datetime.now()
        if (reset_datetime is not None) and (right_now < reset_datetime):
            # add 1 second because of rounding
            seconds_remaining = (reset_datetime - right_now).seconds + 1
        else:
            seconds_remaining = 0

        rate_limit["reset_in_seconds"] = seconds_remaining

        rate_limit["time_to_reset"] = get_readable_time_string(seconds_remaining)
        rate_limits.append(rate_limit)

    return rate_limits