Python requests.Session() Examples

The following are 30 code examples of requests.Session(). 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 , or try the search function .
Example #1
Source File: hdfs_client.py    From incubator-spot with Apache License 2.0 9 votes vote down vote up
def __init__(self, url, mutual_auth, cert=None, verify='true', **kwargs):

        self._logger = logging.getLogger("SPOT.INGEST.HDFS_client")
        session = Session()

        if verify == 'true':
            self._logger.info('SSL verification enabled')
            session.verify = True
            if cert is not None:
                self._logger.info('SSL Cert: ' + cert)
                if ',' in cert:
                    session.cert = [path.strip() for path in cert.split(',')]
                else:
                    session.cert = cert
        elif verify == 'false':
            session.verify = False

        super(SecureKerberosClient, self).__init__(url, mutual_auth, session=session, **kwargs) 
Example #2
Source File: download.py    From DeepLab_v3 with MIT License 8 votes vote down vote up
def download(urls, destination_dir, filenames=None, expected_bytes=None, login_dict=None, force=False):
    with requests.Session() as sess:
        if login_dict:
            sess.post(login_dict['url'], data=login_dict['payload'])
        if isinstance(urls, str):
            return _download(sess, urls, destination_dir, filenames, expected_bytes, force)
        n_urls = len(urls)
        if filenames:
            assert not isinstance(filenames, str) and len(filenames) == n_urls, 'number of filenames does not match that of urls'
        else:
            filenames = [None] * n_urls
        if expected_bytes:
            assert len(expected_bytes) == n_urls, 'number of expected_bytes does not match that of urls'
        else:
            expected_bytes = [None] * n_urls
        return [_download(sess, url, destination_dir, filename, expected_byte, force) for url, filename, expected_byte in zip(urls, filenames, expected_bytes)] 
Example #3
Source File: requests.py    From gql with MIT License 8 votes vote down vote up
def connect(self):

        if self.session is None:

            # Creating a session that can later be re-use to configure custom mechanisms
            self.session = requests.Session()

            # If we specified some retries, we provide a predefined retry-logic
            if self.retries > 0:
                adapter = HTTPAdapter(
                    max_retries=Retry(
                        total=self.retries,
                        backoff_factor=0.1,
                        status_forcelist=[500, 502, 503, 504],
                    )
                )
                for prefix in "http://", "https://":
                    self.session.mount(prefix, adapter)
        else:
            raise TransportAlreadyConnected("Transport is already connected") 
Example #4
Source File: client.py    From pylivy with MIT License 7 votes vote down vote up
def __init__(
        self,
        url: str,
        auth: Auth = None,
        verify: Verify = True,
        requests_session: requests.Session = None,
    ) -> None:

        self.url = url
        self.auth = auth
        self.verify = verify

        if requests_session is None:
            self.session = requests.Session()
            self.managed_session = True
        else:
            self.session = requests_session
            self.managed_session = False 
Example #5
Source File: yts_am_api.py    From yts_torrents with MIT License 7 votes vote down vote up
def requests_retry_session(
    retries=3,
    backoff_factor=0.3,
    status_forcelist=(500, 502, 504),
    session=None,
):
    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 
Example #6
Source File: xq_client.py    From backtrader-cn with GNU General Public License v3.0 7 votes vote down vote up
def __init__(self, **kwargs):
        super(XueQiuClient, self).__init__()
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0',
            'Host': 'xueqiu.com',
            'Pragma': 'no-cache',
            'Connection': 'keep-alive',
            'Accept': '*/*',
            'Accept-Encoding': 'gzip,deflate,sdch',
            'Cache-Control': 'no-cache',
            'Referer': 'http://xueqiu.com/P/ZH003694',
            'X-Requested-With': 'XMLHttpRequest',
            'Accept-Language': 'zh-CN,zh;q=0.8'
        }
        self.session = requests.Session()
        self.session.headers.update(headers)
        self.account_config = None
        self.config.update({
            "create_cubes_url": "https://xueqiu.com/cubes/create.json",
            "get_token_url": "https://xueqiu.com/service/csrf",
            "get_cubes_list": "https://xueqiu.com/v4/stock/portfolio/stocks.json",
            "get_cubes_detail": "https://xueqiu.com/cubes/quote.json",
        }) 
Example #7
Source File: __init__.py    From controller with MIT License 7 votes vote down vote up
def get_session(k8s_api_verify_tls):
    global session
    if session is None:
        with open('/var/run/secrets/kubernetes.io/serviceaccount/token') as token_file:
            token = token_file.read()
        session = requests.Session()
        session.headers = {
            'Authorization': 'Bearer ' + token,
            'Content-Type': 'application/json',
            'User-Agent': user_agent('Deis Controller', deis_version)
        }
        if k8s_api_verify_tls:
            session.verify = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
        else:
            session.verify = False
    return session 
Example #8
Source File: session.py    From mlbv with GNU General Public License v3.0 7 votes vote down vote up
def __init__(self, user_agent, token_url_template, platform):
        self.user_agent = user_agent
        self.token_url_template = token_url_template
        self.platform = platform

        self.session = requests.Session()
        self.session.cookies = http.cookiejar.LWPCookieJar()
        if not os.path.exists(COOKIE_FILE):
            self.session.cookies.save(COOKIE_FILE)
        self.session.cookies.load(COOKIE_FILE, ignore_discard=True)
        self.session.headers = {"User-agent": user_agent}
        if os.path.exists(SESSION_FILE):
            self.load()
        else:
            self._state = {
                'api_key': None,
                'client_api_key': None,
                'token': None,
                'access_token': None,
                'access_token_expiry': None
            }
        self.login() 
Example #9
Source File: qq_spider.py    From QQ_zone with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self.web=webdriver.Chrome()
        self.web.get('https://user.qzone.qq.com')
        config = configparser.ConfigParser(allow_no_value=False)
        config.read('userinfo.ini')
        self.__username =config.get('qq_info','qq_number')
        self.__password=config.get('qq_info','qq_password')
        self.headers={
                'host': 'h5.qzone.qq.com',
                'accept-encoding':'gzip, deflate, br',
                'accept-language':'zh-CN,zh;q=0.8',
                'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
                'connection': 'keep-alive'
        }
        self.req=requests.Session()
        self.cookies={} 
Example #10
Source File: schema.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def from_xml(cls, data, session=None):
        '''
        Create schema field from XML definition

        Parameters
        ----------
        data : xml-string or ElementTree.Element
            The schema field XML definition
        session : requests.Session
            The ESP session object

        Returns
        -------
        :class:`SchemaField`

        '''
        out = cls('', type='double', key=False)
        out.session = session

        if isinstance(data, six.string_types):
            data = ET.fromstring(data)

        out._set_attributes(data.attrib)

        return out 
Example #11
Source File: __init__.py    From wechatpy with MIT License 6 votes vote down vote up
def __init__(
        self,
        appid,
        api_key,
        mch_id,
        sub_mch_id=None,
        mch_cert=None,
        mch_key=None,
        timeout=None,
        sandbox=False,
        sub_appid=None,
    ):
        self.appid = appid
        self.sub_appid = sub_appid
        self.api_key = api_key
        self.mch_id = mch_id
        self.sub_mch_id = sub_mch_id
        self.mch_cert = mch_cert
        self.mch_key = mch_key
        self.timeout = timeout
        self.sandbox = sandbox
        self._sandbox_api_key = None
        self._http = requests.Session() 
Example #12
Source File: component.py    From wechatpy with MIT License 6 votes vote down vote up
def __init__(
        self, component_appid, component_appsecret, component_token, encoding_aes_key, session=None, auto_retry=True,
    ):
        """
        :param component_appid: 第三方平台appid
        :param component_appsecret: 第三方平台appsecret
        :param component_token: 公众号消息校验Token
        :param encoding_aes_key: 公众号消息加解密Key
        """
        self._http = requests.Session()
        self.component_appid = component_appid
        self.component_appsecret = component_appsecret
        self.expires_at = None
        self.crypto = WeChatCrypto(component_token, encoding_aes_key, component_appid)
        self.session = session or MemoryStorage()
        self.auto_retry = auto_retry 
Example #13
Source File: Requests.py    From Vxscan with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
        requests.packages.urllib3.disable_warnings()

        self.timeout = TIMEOUT
        self.session = requests.Session()
        self.headers = get_ua()

        if COOKIE == 'random':
            plain = ''.join([random.choice('0123456789') for _ in range(8)])
            md5sum = hashlib.md5()
            md5sum.update(plain.encode('utf-8'))
            md5 = md5sum.hexdigest()
            self.headers.update({'Cookie': 'SESSION=' + md5})
        else:
            self.headers.update(COOKIE)

        if SOCKS5:
            ip, port = SOCKS5
            socks.set_default_proxy(socks.SOCKS5, ip, port)
            socket.socket = socks.socksocket 
Example #14
Source File: linkdownload.py    From yts_torrents with MIT License 6 votes vote down vote up
def requests_retry_session(
    retries=3,
    backoff_factor=0.3,
    status_forcelist=(500, 502, 504),
    session=None,
):
    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 
Example #15
Source File: hdfs_client.py    From incubator-spot with Apache License 2.0 6 votes vote down vote up
def __init__(self, url, mutual_auth, cert=None, verify='true', **kwargs):

        self._logger = logging.getLogger("SPOT.INGEST.HDFS_client")
        session = Session()

        if verify == 'true':
            self._logger.info('SSL verification enabled')
            session.verify = True
            if cert is not None:
                self._logger.info('SSL Cert: ' + cert)
                if ',' in cert:
                    session.cert = [path.strip() for path in cert.split(',')]
                else:
                    session.cert = cert
        elif verify == 'false':
            session.verify = False

        super(SecureKerberosClient, self).__init__(url, mutual_auth, session=session, **kwargs) 
Example #16
Source File: image_helper.py    From JJMumbleBot with GNU General Public License v3.0 6 votes vote down vote up
def download_image_requests_to_dir(img_url, dir_name):
    dir_utils.clear_directory(f'{dir_utils.get_temp_med_dir()}/{dir_name}')
    img_ext = img_url.rsplit('.', 1)[1]
    s = requests.Session()
    r = s.get(img_url)
    if r.status_code == 200:
        with open(f"{dir_utils.get_temp_med_dir()}/{dir_name}/_image.{img_ext}", 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
        dprint(f"Downloaded image from: {img_url}")
    else:
        dprint(f"{r.status_code} Error! - {img_url}")
    if img_ext == 'png':
        dprint(f"Fixing image to force jpg conversion: {img_url}")
        img_fix = Image.open(f"{dir_utils.get_temp_med_dir()}/{dir_name}/_image.{img_ext}")
        img_fix.convert('RGB').save(f"{dir_utils.get_temp_med_dir()}/{dir_name}/_image.jpg")
        dir_utils.remove_file("_image.png", f'{dir_utils.get_temp_med_dir()}/{dir_name}')
    dprint(f"Downloaded image from: {img_url}") 
Example #17
Source File: sectoralarm.py    From sectoralarm with MIT License 6 votes vote down vote up
def __login(self):
        '''
        Login to the site if we're not logged in already. First try any
        existing session from the stored cookie. If that fails we should
        login again.
        '''
        self.__load_cookies()

        if not self.__is_logged_in():
            log('Logging in')
            form_data = {
                'userID': self.config.email,
                'password': self.config.password
            }
            self.session = requests.Session()
            # Get CSRF-token and add it to the form data.
            form_data['__RequestVerificationToken'] = self.__get_token()

            # Do the actual logging in.
            self.session.post(LOGINPAGE + '?Returnurl=~%2F', data=form_data)

            # Save the cookies to file.
            self.__save_cookies()
        else:
            log('Already logged in') 
Example #18
Source File: realtime.py    From twstock with MIT License 6 votes vote down vote up
def get_raw(stocks) -> dict:
    req = requests.Session()
    req.get(SESSION_URL, proxies=get_proxies())

    r = req.get(
        STOCKINFO_URL.format(
            stock_id=_join_stock_id(stocks),
            time=int(time.time()) * 1000))

    if sys.version_info < (3, 5):
        try:
            return r.json()
        except ValueError:
            return {'rtmessage': 'json decode error', 'rtcode': '5000'}
    else:
        try:
            return r.json()
        except json.decoder.JSONDecodeError:
            return {'rtmessage': 'json decode error', 'rtcode': '5000'} 
Example #19
Source File: app_credentials.py    From botbuilder-python with MIT License 6 votes vote down vote up
def signed_session(self, session: requests.Session = None) -> requests.Session:
        """
        Gets the signed session.  This is called by the msrest package
        :returns: Signed requests.Session object
        """
        if not session:
            session = requests.Session()

        if not self._should_authorize(session):
            session.headers.pop("Authorization", None)
        else:
            auth_token = self.get_access_token()
            header = "{} {}".format("Bearer", auth_token)
            session.headers["Authorization"] = header

        return session 
Example #20
Source File: comm.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def router_login(base_url):
    """
    Prompt for router username and password and attempt login.

    Returns the username if successful or None.
    """
    config = PdtoolsConfig.load()
    session = requests.Session()
    url_parts = urlparse(base_url)

    for username, password in LoginGatherer(url_parts.netloc):
        # Try to get a token for later use. Prior to 1.10, paradrop-daemon
        # does not not support tokens.
        _, token = send_router_login(base_url, username, password, session)
        if token is not None:
            config.addAccessToken(url_parts.netloc, username, token)
            config.save()
            return username

    return None 
Example #21
Source File: promenade_client.py    From drydock with Apache License 2.0 6 votes vote down vote up
def __init__(self, scheme='http', marker=None, timeout=None):
        self.logger = logging.getLogger(__name__)
        self.__session = requests.Session()

        self.set_auth()

        self.marker = marker
        self.__session.headers.update({'X-Context-Marker': marker})

        self.prom_url = self._get_prom_url()
        self.port = self.prom_url.port
        self.host = self.prom_url.hostname
        self.scheme = scheme

        if self.port:
            self.base_url = "%s://%s:%s/api/" % (self.scheme, self.host,
                                                 self.port)
        else:
            # assume default port for scheme
            self.base_url = "%s://%s/api/" % (self.scheme, self.host)

        self.default_timeout = self._calc_timeout_tuple((20, 30), timeout) 
Example #22
Source File: client.py    From clashroyale with MIT License 6 votes vote down vote up
def __init__(self, token, session=None, is_async=False, **options):
        self.token = token
        self.is_async = is_async
        self.error_debug = options.get('error_debug', False)
        self.timeout = options.get('timeout', 10)
        self.api = API(options.get('url', 'https://api.royaleapi.com'))
        self.session = session or (aiohttp.ClientSession() if is_async else requests.Session())
        self.camel_case = options.get('camel_case', False)
        self.headers = {
            'Authorization': 'Bearer {}'.format(token),
            'User-Agent': 'python-clashroyale-client (fourjr/kyb3r) ' + options.get('user_agent', '')
        }
        self.cache_fp = options.get('cache_fp')
        self.using_cache = bool(self.cache_fp)
        self.cache_reset = options.get('cache_expires', 300)
        self.ratelimit = [10, 10, 0]
        if self.using_cache:
            table = options.get('table_name', 'cache')
            self.cache = SqliteDict(self.cache_fp, table) 
Example #23
Source File: app_credentials.py    From botbuilder-python with MIT License 5 votes vote down vote up
def _should_authorize(
        self, session: requests.Session  # pylint: disable=unused-argument
    ) -> bool:
        return True 
Example #24
Source File: test_session.py    From Jike-Metro with MIT License 5 votes vote down vote up
def test_init(self):
        self.assertIsInstance(self.jike_session.session, requests.Session)
        self.assertEqual(self.jike_session.token, 'token')
        self.assertEqual(self.jike_session.headers['x-jike-app-auth-jwt'], 'token') 
Example #25
Source File: schema.py    From python-esppy with Apache License 2.0 5 votes vote down vote up
def from_xml(cls, data, session=None):
        '''
        Create schema from XML definition

        Parameters
        ----------
        data : xml-string or ElementTree.Element
            The schema XML definition
        session : requests.Session
            The ESP session object

        Returns
        -------
        :class:`Schema`

        '''
        out = cls()
        out.session = session

        if isinstance(data, six.string_types):
            data = xml.from_xml(data)

        out._set_attributes(data.attrib)

        for item in data.findall('.//field'):
            field = SchemaField.from_xml(item)
            out.fields[field.name] = field

        return out 
Example #26
Source File: luis_recognizer_test.py    From botbuilder-python with MIT License 5 votes vote down vote up
def _get_recognizer_result(
        cls,
        utterance: str,
        response_json: Union[str, Dict[str, object]],
        bot_adapter: BotAdapter = TestAdapter(),
        options: LuisPredictionOptions = None,
        include_api_results: bool = False,
        telemetry_properties: Dict[str, str] = None,
        telemetry_metrics: Dict[str, float] = None,
        recognizer_class: type = LuisRecognizer,
    ) -> Tuple[LuisRecognizer, RecognizerResult]:
        if isinstance(response_json, str):
            response_json = LuisRecognizerTest._get_json_for_file(
                response_file=response_json
            )

        recognizer = LuisRecognizerTest._get_luis_recognizer(
            recognizer_class, include_api_results=include_api_results, options=options
        )
        context = LuisRecognizerTest._get_context(utterance, bot_adapter)
        response = Mock(spec=Response)
        response.status_code = 200
        response.headers = {}
        response.reason = ""
        with mock.patch.object(Session, "send", return_value=response):
            with mock.patch.object(
                Deserializer, "_unpack_content", return_value=response_json
            ):
                result = await recognizer.recognize(
                    context, telemetry_properties, telemetry_metrics
                )
                return recognizer, result 
Example #27
Source File: autogender.py    From grimoirelab-sortinghat with GNU General Public License v3.0 5 votes vote down vote up
def genderize(name, api_token=None):
    """Fetch gender from genderize.io"""

    GENDERIZE_API_URL = "https://api.genderize.io/"
    TOTAL_RETRIES = 10
    MAX_RETRIES = 5
    SLEEP_TIME = 0.25
    STATUS_FORCELIST = [502]

    params = {
        'name': name
    }

    if api_token:
        params['apikey'] = api_token

    session = requests.Session()

    retries = urllib3.util.Retry(total=TOTAL_RETRIES,
                                 connect=MAX_RETRIES,
                                 status=MAX_RETRIES,
                                 status_forcelist=STATUS_FORCELIST,
                                 backoff_factor=SLEEP_TIME,
                                 raise_on_status=True)

    session.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries))
    session.mount('https://', requests.adapters.HTTPAdapter(max_retries=retries))

    r = session.get(GENDERIZE_API_URL, params=params)
    r.raise_for_status()
    result = r.json()

    gender = result['gender']
    prob = result.get('probability', None)

    acc = int(prob * 100) if prob else None

    return gender, acc 
Example #28
Source File: twitch-viewer.py    From twitch-viewer with Apache License 2.0 5 votes vote down vote up
def open_url(url, proxy):
    # Sending HEAD requests
    while True:
        try:
            with requests.Session() as s:
                response = s.head(url, proxies=proxy)
            print "Sent HEAD request with %s" % proxy["http"]
            time.sleep(20)
        except requests.exceptions.Timeout:
            print "  Timeout error for %s" % proxy["http"]
        except requests.exceptions.ConnectionError:
            print "  Connection error for %s" % proxy["http"] 
Example #29
Source File: session.py    From Jike-Metro with MIT License 5 votes vote down vote up
def __init__(self, token):
        self.session = requests.Session()
        self.token = token
        self.headers = dict(HEADERS)
        self.headers.update({'x-jike-app-auth-jwt': token}) 
Example #30
Source File: download_from_google_drive.py    From ciftify with MIT License 5 votes vote down vote up
def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)