def get_response(name, request_kwargs): host = Host(request_kwargs['headers'].get('Host', None) or urlparse(request_kwargs['url']).netloc.split('@')[-1]) session = Session(host, name) session.load() # Update session headers with the request headers. session['headers'].update(request_kwargs.get('headers', {})) # Use the merged headers for the request request_kwargs['headers'] = session['headers'] auth = request_kwargs.get('auth', None) if auth: session.auth = auth elif session.auth: request_kwargs['auth'] = session.auth rsession = RSession(cookies=session.cookies) try: response = rsession.request(**request_kwargs) except Exception: raise else: session.cookies = rsession.cookies session.save() return response
def try_authenticate(self, payload): for _ in range(5): s = RSession() s.post('{}/forum/login.php'.format(BASE_URL), data=payload) if s.cookies and len(s.cookies) > 0: return s.cookies else: sleep(3) raise PluginError('unable to obtain cookies from rutracker')
def __init__(self, country='US', short_lang='en', long_lang=None, tld=None, currency=None, cache_dir=None, cache_file=None): """ :param country: Two-letter ("ISO 3166-1 alpha-2") country code; defaults to 'US'. :param short_lang: Two-letter ("ISO 639-1") language code; defaults to 'en'. :param long_lang: Language + country ("RFC5646"); defaults to {short_lang}_{country}. :param tld: Top-level domain suffix to 'digikey' hostname; defaults to 'com' if country is US, or the country otherwise. :param currency: ISO 4217 code. As a poor approximation, this defaults to {country} + 'D'. :param cache_dir: Place to store metadata cache. Defaults to '.digikey'. :param cache_file: Name of metadata cache file. Defaults to 'session.pickle.xz'. """ from requests import Session as RSession self._rsession = RSession() # Some fairly poor guesses if not long_lang: long_lang = '%s_%s' % (short_lang, country) if not tld: tld = 'com' if country == 'US' else country.lower() if not currency: currency = country + 'D' self.country, self.short_lang, self.long_lang, self.tld, self.currency = \ country, short_lang, long_lang, tld, currency self.base = 'https://www.digikey.' + tld self.set_locale() self._rsession.cookies.update({ 'SiteForCur': country, 'cur': currency, 'website#lang': long_lang }) self._rsession.headers.update({ 'Accept-Language': '%s,%s;q=0.9' % (long_lang, short_lang), 'Referer': self.base, 'User-Agent': 'Mozilla/5.0' }) self.categories = {} self.groups = {} self.cache_dir, self.cache_file = Session._cache_defaults( cache_dir, cache_file) self.shared_params = {} # Not initialized until init_groups() super().__init__(session=self, title='All', path='products/' + short_lang)
def update_base_url(): url = None for mirror in MIRRORS: try: s = RSession() response = s.get(mirror, timeout=2) if response.ok: url = mirror break except RequestException as err: log.debug('Connection error. %s', str(err)) if url: return url else: raise PluginError('Host unreachable.')
def __init__( self, country='US', short_lang='en', long_lang: str = None, tld: str = None, currency: str = None, ): """ :param country: Two-letter ("ISO 3166-1 alpha-2") country code; defaults to 'US'. :param short_lang: Two-letter ("ISO 639-1") language code; defaults to 'en'. :param long_lang: Language + country ("RFC5646"); defaults to {short_lang}_{country}. :param tld: Top-level domain suffix to 'digikey' hostname; defaults to 'com' if country is US, or the country otherwise. :param currency: ISO 4217 code. As a poor approximation, this defaults to {country} + 'D'. """ from requests import Session as RSession self._rsession = RSession() self.country, self.short_lang, self.long_lang, self.tld, self.currency = \ self._lang_defaults(country, short_lang, long_lang, tld, currency) self.base = 'https://www.digikey.' + self.tld self.set_locale() self._rsession.cookies.update({ 'SiteForCur': country, 'cur': self.currency, 'website#lang': self.long_lang }) self._rsession.headers.update({ 'Accept-Language': '%s,%s;q=0.9' % (self.long_lang, self.short_lang), 'Referer': self.base, 'User-Agent': 'Mozilla/5.0' }) self.groups: Dict[str, Group] = {} self.categories: Dict[str, Category] = {} self.shared_params: Dict[str, Param] = { } # Not initialized until init_groups() super().__init__(session=self, title='All', path='products/' + self.short_lang) self._bake_cookies()
def handle(self, request): session = RSession() url = self.__apiUrl + request.getPath() req = RRequest(request.getMethod(), url, headers=self.buildHeader(request)) prepped = req.prepare() prepped.body = request.getBody() resp = session.send(prepped, verify=True) json_resp = None try: json_resp = json.loads(resp.content) except: pass self.checkResponse(json_resp, resp.headers["content-type"]) return json_resp