def init(proxy: str, cookie: str) -> None: global _requester _requester.cookies.set_policy(_BlockCookiesSet()) _requester.mount( "http://", HTTPAdapter( max_retries=Retry(total=3, read=5, connect=5, backoff_factor=0.3), pool_maxsize=50, ), ) _requester.mount( "https://", HTTPAdapter( max_retries=Retry(total=3, read=5, connect=5, backoff_factor=0.3), pool_maxsize=50, ), ) if proxy is not None and len(proxy) > 0: # we have a proxy, set it if not proxy.startswith("http"): proxy = f"http://{proxy}" proxies = {"http": proxy, "https": proxy} _requester.proxies.update(proxies) if cookie is not None and len(cookie) > 0: name = cookie.split("=")[0] val = cookie.split("=")[1] c = requests.cookies.create_cookie(name=name, value=val) _requester.cookies.set_cookie(c)
def __init__( self, total=10, read=10, connect=10, status=10, backoff_factor=5, status_forcelist=(500, 502, 503, 504), allowed_methods=False, method_whitelist=False, # preserved for backwards compatibility pretty_print=False, **kwargs, ): self.status_forcelist = status_forcelist self.pretty_print = pretty_print self.logger = logging.getLogger(type(self).__name__) super().__init__() # we'll force raise_on_status to False so we can get the server response, # because otherwise urllib3 hides the error body after retry exhaustion kwargs.pop("raise_on_status", None) # environmental retries override (useful for testing) total = int(os.environ.get("MAX_RETRIES_ON_CONN_ERROR", total)) allowed_methods = allowed_methods or method_whitelist try: retry = Retry( total=total, read=read, connect=connect, backoff_factor=backoff_factor, status_forcelist=status_forcelist, allowed_methods=allowed_methods, raise_on_status=False, **kwargs, ) except TypeError: retry = Retry( total=total, read=read, connect=connect, backoff_factor=backoff_factor, status_forcelist=status_forcelist, method_whitelist=allowed_methods, raise_on_status=False, **kwargs, ) adapter = HTTPAdapter(max_retries=retry) self.mount("http://", adapter) self.mount("https://", adapter)
def getskills(hostname, username, password, userid): # retrieves the skills and skillevel attributed to a specific agent # Loading Resources ... # this snippet implements backoff timer and retries when hammering the UCCX with api requests session = requests.Session() retry = Retry(connect=3, backoff_factor=0.5) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) resources = session.get('https://' + hostname + '/adminapi/resource/' + userid, auth=HTTPBasicAuth(username, password), verify=False) # 200 if success # Creating Tree... root = ET.fromstring(resources.content) # Putting relevant values as a dictionary in a list for improved ease of handling ... skills = [] # in contrary to findall, iter search in all sub-elements, not only in the directly descending ones of the root for skill in root.iter('skillCompetency'): #debug information #print(skill.find('competencelevel').text) #print(skill.find('skillNameUriPair').attrib['name']) skills.append({'level': skill.find('competencelevel').text, 'name': skill.find('skillNameUriPair').attrib['name']}) return skills
def getFolderArrayResponse(artist, bool, folder, accesstoken, offset): """ Method ran to get the list of folders from an artist from deviantart's API. :param artist: The artist's name that owns the folder. :type artist: string :param bool: Whether mature folders will show or not. :type bool: bool :param folder: The Exact folder name to grab the UUID of :type folder: string :param accesstoken: The DA Access token to use for this query :type accesstoken: string :param offset: The offset value at which to request the gallery folder contents from. The starting value :type offset: int :return: array """ finished = False retries = Retry(connect=5, read=2, redirect=5, backoff_factor=3) logger = logging.getLogger('errite.da.daparser') logger.info("GetFolderArrayResponse: Started") with open("artdata.json", "r") as jsonFile: logger.debug("GetFolderArray: Offset:" + str(offset)) folderRequestURL = "https://www.deviantart.com/api/v1/oauth2/gallery/folders?access_token=" + accesstoken + "&username="******"&calculate_size=false&ext_preload=false&limit=10&mature_content=" + convertBoolString( bool) + "&offset=" + str(offset) # print(folderRequestURL) http = PoolManager(retries=retries) heroes = http.request('GET', folderRequestURL) data = json.loads(heroes.data.decode('UTF-8')) return data
def extract_references(self, filename: str) -> List[Reference]: """ Extract references from the PDF represented by ``filehandle``. Parameters ---------- filename : str Returns ------- list Items are :class:`.Reference` instances. """ self._adapter.max_retries = Retry(connect=30, read=10, backoff_factor=20) try: _target = urljoin(self.endpoint, self.path) response = self._session.post( _target, files={'input': open(filename, 'rb')}) except requests.exceptions.ConnectionError as e: raise IOError('%s: GROBID extraction failed: %s' % (filename, e)) if not response.ok: raise IOError('%s: GROBID extraction failed: %s' % (filename, response.content)) return format_grobid_output(response.content)
def __requests_retry_session(self, backoff_factor=0.5, status_forcelist=(429, 502, 503)): """ Create a Requests session that uses automatic retries. :param backoff_factor: Backoff factor used to calculate time between retries. :type backoff_factor: float :param status_forcelist: A tuple containing the response status codes that should trigger a retry. :type status_forcelist: tuple :return: Requests Session :rtype: Request """ session = requests.Session() retry = Retry(total=self.max_retries, read=self.max_retries, connect=self.max_retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist, method_whitelist=frozenset([ ApiRequestHandler.GET, ApiRequestHandler.POST, ApiRequestHandler.PUT, ApiRequestHandler.DELETE ])) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) return session
def scrap_work_mma(): session = requests.Session() # 네트워크 통신에 실패했을 경우 {backoff factor} * (2 ^ ({number of total retries} - 1))초 이후 재시도 retry = Retry( total=10, read=10, connect=5, backoff_factor=0.3, status_forcelist=(500, 502, 504), ) adapter = HTTPAdapter(max_retries=retry) session.mount("http://", adapter) session.mount("https://", adapter) session.headers.update({ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) " "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" }) r = session.post( "https://work.mma.go.kr/caisBYIS/board/boardList.do", data={ "gesipan_gbcd": 13, "tmpl_id": 1, "menu_id": "m_m8_6", "pageUnit": 999_999, "pageIndex": 1, }, )
def setup_configuration( api_key=None, host="", username=None, password=None, verify_ssl=True ): host = host + "/v1" global config if api_key is None: api_key = {} config.api_key = api_key config.host = host config.username = username config.password = password config.verify_ssl = verify_ssl config.retries = Retry( total=10, backoff_factor=0.25, status_forcelist=[503], allowed_methods=[ "HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE", "POST", "PATCH", ], raise_on_status=False, # Don't remove any headers on redirect remove_headers_on_redirect=[], ) # Set logged in at this point global logged_in logged_in = True
def request_route_to_server(origin, destination): """ Queries the OSRM for a path. Args: origin (list): origin coordinate (longitude, latitude) destination (list): target coordinate (longitude, latitude) Returns: list, float, float = the path, the distance of the path and the estimated duration """ try: url = "http://osrm.gti-ia.upv.es/route/v1/car/{src1},{src2};{dest1},{dest2}?geometries=geojson&overview=full" src1, src2, dest1, dest2 = origin[1], origin[0], destination[1], destination[0] url = url.format(src1=src1, src2=src2, dest1=dest1, dest2=dest2) session = requests.Session() retry = Retry(connect=3, backoff_factor=1.0) adapter = HTTPAdapter(max_retries=retry) session.mount(url, adapter) result = session.get(url) result = json.loads(result.content) path = result["routes"][0]["geometry"]["coordinates"] path = [[point[1], point[0]] for point in path] duration = result["routes"][0]["duration"] distance = result["routes"][0]["distance"] if path[-1] != destination: path.append(destination) return path, distance, duration except Exception: return None, None, None
def extract_references(self, filename: str) -> List[Reference]: """ Extract references from the PDF represented by ``filehandle``. Parameters ---------- filename : str Returns ------- list Items are :class:`.Reference` instances. """ # This can take a while. self._adapter.max_retries = Retry(connect=30, read=10, backoff_factor=20) _target = urljoin(self.endpoint, '/cermine/extract') try: response = self._session.post(_target, files={'file': open(filename, 'rb')}) except requests.exceptions.ConnectionError as e: raise IOError('%s: CERMINE extraction failed: %s' % (filename, e)) if not response.ok: raise IOError('%s: CERMINE extraction failed: %s' % (filename, response.content)) return cxml_to_json(response.content)
def retryable_request(retries: Optional[Union[int, Retry]] = 1, *, session: Session = None, backoff_factor: float = None) -> Session: """Возвращает объект Session, который способен повторять запрос указанное число раз. :param retries: Количество повторов. :param session: Объект сессии. :param backoff_factor: Увеличивающаяся задержка между повторами. """ assert retries is None or isinstance(retries, (int, Retry)) retries = 1 if retries is None else retries backoff_factor = 0 if backoff_factor is None else backoff_factor session = Session() if session is None else session if isinstance(retries, int): retry = Retry(total=retries, read=retries, connect=retries, backoff_factor=backoff_factor) else: retry = retries adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) return session
def requests_retry_session( retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None, ): """ Drop-in replacement for requests.get with support for retries. Uses an exponential backoff algorithm to sleep between retry attempts. If the backoff_factor is 0.1, will sleep for [0.0s, 0.2s, 0.4s, ...] between retries. :param retries: Total number of retries to allow. :param backoff_factor: A backoff factor to apply between attempts. :param status_forcelist: HTTP status codes to force a retry on. :param session: :return: """ if session is None: session = requests.Session() retry = Retry( total=retries, read=retries, connect=retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist, ) adapter = HTTPAdapter(max_retries=retry) session.mount("http://", adapter) session.mount("https://", adapter) return session
def get_viewers(): session = requests.Session() retry = Retry(connect=500, backoff_factor=0.5) adapter = HTTPAdapter(max_retries=retry) session.mount("http://", adapter) session.mount("https://", adapter) try: channel_json = session.get(url="https://tmi.twitch.tv/group/user/" + encryption_key.decrypted_chan + "/chatters").json() broadcaster = channel_json["chatters"]["broadcaster"] viewers = channel_json["chatters"]["viewers"] moderators = channel_json["chatters"]["moderators"] staff = channel_json["chatters"]["staff"] vips = channel_json["chatters"]["vips"] global_mods = channel_json["chatters"]["global_mods"] admins = channel_json["chatters"]["admins"] viewers_list = viewers + staff + vips + global_mods + admins viewers_and_mods = [viewers_list, moderators + broadcaster] return viewers_and_mods except TypeError as e: logging_line(e) return ( general.get_viewers_func ) # shitty workaround, dont know if this will cause it to get stuck
class API(AbstractQueryAPI): name = '必应 API' timeout = 10 headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' } retries = Retry(total=5, backoff_factor=3, status_forcelist=[500, 502, 503, 504]) session = requests.Session() session.mount('http://', HTTPAdapter(max_retries=retries)) session.mount('https://', HTTPAdapter(max_retries=retries)) url = 'http://xtk.azurewebsites.net/BingDictService.aspx' parser = Parser @classmethod def query(cls, word) -> dict: validator = str.maketrans( string.punctuation, ' ' * len(string.punctuation)) # 第三方Bing API查询包含标点的单词时有可能会报错,所以用空格替换所有标点 query_result = None try: rsp = cls.session.get(cls.url, params=urlencode( {'Word': word.translate(validator)}), timeout=cls.timeout) logger.debug( f'code:{rsp.status_code}- word:{word} text:{rsp.text}') query_result = cls.parser(rsp.json(), word).result except Exception as e: logger.exception(e) finally: logger.debug(query_result) return query_result
def _set_client(self): if self._connect: try: retry = Retry(total=self._total_retry, connect=self._connect_retry, read=self._read_retry, redirect=False, status=self._status_retry, status_forcelist=[403], backoff_factor=self._backoff_factor, raise_on_status=False, respect_retry_after_header=True) retry.RETRY_AFTER_STATUS_CODES = retry.RETRY_AFTER_STATUS_CODES | { 403 } retry.BACKOFF_MAX = self._backoff_max self._debug('Creating client ...') self._client = Github(login_or_token=self._user, password=self._passw, base_url=self._url, retry=retry) self._debug('Client created') self._debug('Getting rate limit ...') limit = self._client.get_rate_limit().search.limit self._debug('Rate limit per minute', limit) self._delay = 60 / limit self._debug('Delay time', self._delay) except BadCredentialsException as e: self._authentication_critical(e) except ConnectionError as e: self._connection_critical(e) else: self._delay = 6 # take the delay as if it is not authenticated
def _get_retry_config(self): retry = Retry(total=20, backoff_factor=0.22, status_forcelist=[409, 500, 502, 503, 504], allowed_methods=frozenset(['POST', 'PUT', 'DELETE'])) retry.BACKOFF_MAX = 10 return retry
def get_session(): """ Create a requests session to access the REST API :return: requests session :rtype: Session """ config = PyquilConfig() session = requests.Session() retry_adapter = HTTPAdapter( max_retries=Retry(total=3, method_whitelist=['POST'], status_forcelist=[502, 503, 504, 521, 523], backoff_factor=0.2, raise_on_status=False)) session.mount("http://", retry_adapter) session.mount("https://", retry_adapter) # We need this to get binary payload for the wavefunction call. session.headers.update({ "Accept": "application/octet-stream", "X-User-Id": config.user_id, "X-Api-Key": config.api_key }) session.headers.update({'Content-Type': 'application/json; charset=utf-8'}) return session
class API(AbstractQueryAPI): name = '欧陆词典 API' timeout = 10 headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' } retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504]) session = requests.Session() session.mount('http://', HTTPAdapter(max_retries=retries)) session.mount('https://', HTTPAdapter(max_retries=retries)) url = 'https://dict.eudic.net/dicts/en/{}' parser = Parser @classmethod def query(cls, word) -> dict: queryResult = None try: rsp = cls.session.get(cls.url.format(word), timeout=cls.timeout) logger.debug( f'code:{rsp.status_code}- word:{word} text:{rsp.text}') queryResult = cls.parser(rsp.text, word).result except Exception as e: logger.exception(e) finally: logger.debug(queryResult) return queryResult
def __init__( self, retries: int = 3, backoff_factor: float = 0.3, status_to_retry: Tuple[int, ...] = (500, 502, 504), prefixes: Tuple[str, ...] = ("https://", ), ) -> None: self.session = Session() self.retries = retries retry_args = { "total": retries, "read": retries, "connect": retries, "backoff_factor": backoff_factor, "status_forcelist": status_to_retry, } if hasattr(Retry.DEFAULT, "allowed_methods"): retry_args.update({"allowed_methods": False}) else: retry_args.update({"method_whitelist": False}) r = Retry(**retry_args) adapter = HTTPAdapter(max_retries=r) for prefix in prefixes: self.session.mount(prefix, adapter) self.session.hooks = { "response": [lambda r, *args, **kwargs: r.raise_for_status()] }
def get_gspread_connection( credentials: service_account.Credentials, max_retries=7, backoff_factor=0.5, status_forcelist=frozenset([403, 413, 429, 503]), ) -> Client: scoped_credentials = credentials.with_scopes([ "https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive", ]) retry = Retry( total=max_retries, read=max_retries, connect=max_retries, status=max_retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist, ) adapter = HTTPAdapter(max_retries=retry) session = Session() session.mount("https://", adapter) gc = Client(auth=scoped_credentials, session=session) gc.session = AuthorizedSession(scoped_credentials) return gc
def get_session(retries=5, backoff_factor=1, status_forcelist=(500, 502, 504), tor_session=None): s = requests.Session() # Should be an empty dictionary if not using tor if not tor_session: proxies = {} else: proxies = tor_session.proxies s.headers.update({'User-Agent': get_random_user_agent()}) retries = Retry(total=retries, read=retries, connect=retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist) adapter = HTTPAdapter(max_retries=retries) s.mount("http://", adapter) s.mount("https://", adapter) s.proxies = proxies s.cookies.set_policy(BlockAll()) return s
def _build_http_session(self, retry_attempts: int, retry_status_codes: List[int], retry_delay: float) -> Session: r""" Build the HTTP Session with a retry strategy using an HTTP Adapter. :param retry_attempts: The maximum number of retries to be attempted, in case of the provided retry_status_codes. The value is capped at 5. :param retry_status_codes: The list of status codes for which retry should be attempted. :param retry_delay: The base delay/backoff factor for exponential backoff. """ max_retry_attempts = retry_attempts \ if retry_attempts < self.MAX_RETRY_ATTEMPTS \ else self.MAX_RETRY_ATTEMPTS retry = Retry(total=2 * self.MAX_RETRY_ATTEMPTS, read=False, status=max_retry_attempts, status_forcelist=retry_status_codes, backoff_factor=retry_delay) http_adapter = HTTPAdapter(max_retries=retry) http_session = Session() http_session.mount("https://", http_adapter) return http_session
def _request_session( self, retries=3, backoff_factor=0.3, status_forcelist=(408, 500, 502, 503, 504, 520, 521, 522, 523, 524), ): """Get a session with Retry enabled. :param retries: Number of retries to allow. :param backoff_factor: Backoff factor to apply between attempts. :param status_forcelist: HTTP status codes to force a retry on. :returns: A requests.Session object. """ session = requests.Session() retry = Retry( total=retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist, method_whitelist=False, raise_on_redirect=False, raise_on_status=False, respect_retry_after_header=False, ) adapter = HTTPAdapter(max_retries=retry) session.mount("http://", adapter) session.mount("https://", adapter) return session
def rate(self): url = "http://osrm.gti-ia.upv.es/route/v1/car/{src1},{src2};{dest1},{dest2}?geometries=geojson&overview=full" src1, src2, dest1, dest2 = self.init_position[1], self.init_position[0], self.dest[1], self.dest[0] url = url.format(src1=src1, src2=src2, dest1=dest1, dest2=dest2) session = requests.Session() retry = Retry(connect=3, backoff_factor=1.0) adapter = HTTPAdapter(max_retries=retry) session.mount(url, adapter) result = session.get(url) result = json.loads(result.content) distance = result["routes"][0]["distance"] run_time = self.total_time() - self.get_waiting_time() if run_time < distance/694.0: return 5 elif run_time < distance/527.0: return 4 elif run_time < distance/416.0: return 3 elif run_time < distance/347.0: return 2 elif run_time < distance/277.0: return 1 else: return 0
def requests_retry_session( retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None, ): """ Retry policy :param retries: int :param backoff_factor: float :param status_forcelist: tuple of ints :param session: requests.Session :return: requests.Session """ session = session or requests.Session() retry = Retry( total=retries, read=retries, connect=retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist, ) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) return session
def perform_search( request_url: str, query: str, search_url: str = GOOGLE_SEARCH_URL, search_country: Optional[str] = None, headers: Optional[Dict[str, Any]] = None, total: int = 3, backoff_factor: int = 3, status_force_list: Optional[List[int]] = None, ) -> requests.Response: params = {"q": query} if search_country: params["cr"] = search_country if not status_force_list: status_force_list = [404, 429] retries = Retry( total=total, backoff_factor=backoff_factor, status_forcelist=status_force_list, ) with requests.Session() as s: s.mount(request_url, HTTPAdapter(max_retries=retries)) return s.get(search_url, headers=headers, params=params)
def get_session( self, retries=3, backoff_factor=0.3, status_forcelist=(429, 500, 502, 503, 504), session=None, ): """ Method for returning a session object per every requesting thread """ if not hasattr(thread_local, "session"): session = session or requests.Session() retry = Retry( total=retries, read=retries, connect=retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist, ) adapter = HTTPAdapter(max_retries=retry) session.mount("http://", adapter) session.mount("https://", adapter) thread_local.session = session return thread_local.session
def requests_retry_session(retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None): """Return a python-requests Session that retries on HTTP failure. For usage see https://www.peterbe.com/plog/best-practice-with-retries-with-requests. Args: retries: optional int, number of retries to attempt. backoff_factor: See https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#module-urllib3.util.retry. status_forcelist: optional list of HTTP status codes that will trigger a retry. session: optional pre-built requests.Session object. Returns: A requests.Session object we can use to call .get(), post() etc. """ session = session or requests.Session() retry = Retry( total=retries, read=retries, connect=retries, backoff_factor=backoff_factor, status_forcelist=status_forcelist, ) adapter = HTTPAdapter(max_retries=retry) session.mount("http://", adapter) session.mount("https://", adapter) return session
def getGalleryFolderArrayResponse(artist, bool, folder, accesstoken, offset): """ Method ran to get the Gallery Folder data from deviantart's API. :param artist: The artist's name that owns the folder. :type artist: string :param bool: Whether mature folders will show or not. :type bool: bool :param folder: UUID of the folder that data is being grabbed from :type folder: string :param accesstoken: The DA Access token to use for this query :type accesstoken: string :param offset: The offset value at which to request the gallery folder contents from. The starting value :type offset: int :return: array """ finished = False retries = Retry(connect=5, read=5, redirect=5, backoff_factor=4) folderRequestURL = "https://www.deviantart.com/api/v1/oauth2/gallery/" + folder + "?username="******"&access_token=" + accesstoken + "&limit=10&mature_content=" + convertBoolString( bool) + "&offset=" + str(offset) # print(offset) http = PoolManager(retries=retries) heroes = http.request('GET', folderRequestURL) data = json.loads(heroes.data.decode('UTF-8')) return data
def __init__(self, access_token, default_timeout=(10, 60), base_url=None): super().__init__() if isinstance(access_token, HiddenToken): access_token = access_token.token self.headers = { 'Authorization': f'Bearer {access_token}', 'Accept': 'application/json', 'Accept-Encoding': 'gzip,deflate' } self.default_timeout = default_timeout self.base_url = base_url or BASE_URL # set requests to automatically retry retries = Retry(total=10, connect=3, read=3, status=3, status_forcelist=[ 408, 423, 444, 500, 501, 502, 503, 504, 507, 508, 511, 599 ], backoff_factor=0.5, raise_on_status=False, remove_headers_on_redirect=[]) adapter = requests.adapters.HTTPAdapter(max_retries=retries) self.mount(self.base_url, adapter)