Python requests.session() Examples

The following are 30 code examples for showing how to use requests.session(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module requests , or try the search function .

Example 1
Project: cloudflare-tor-whitelister   Author: DonnchaC   File: whitelist.py    License: GNU General Public License v2.0 6 votes vote down vote up
def fetch_access_rules(session, page_num=1, zone_id=None, per_page=50):
    """
    Fetch current access rules from the CloudFlare API
    """

    # If zone_id, only apply rule to current zone/domain
    params = {'page': page_num, 'per_page': per_page}
    if zone_id:
        r = session.get('https://api.cloudflare.com/client/v4/zones/{}'
                        '/firewall/access_rules/rules'.format(
                            zone_id), params=params)
    else:
        r = session.get('https://api.cloudflare.com/client/v4/user'
                        '/firewall/access_rules/rules', params=params)
    r.raise_for_status()
    res = r.json()
    if not res['success']:
        raise CloudFlareAPIError(res['errors'])
    else:
        return res 
Example 2
Project: cloudflare-tor-whitelister   Author: DonnchaC   File: whitelist.py    License: GNU General Public License v2.0 6 votes vote down vote up
def remove_access_rule(session, rule_id, zone_id=None):
    """
    Remove an existing access rule via the CloudFlare API
    """
    if zone_id:
        r = session.delete('https://api.cloudflare.com/client/v4/zones/{}'
                           '/firewall/access_rules/rules/{}'.format(
                               zone_id, rule_id))
    else:
        # Apply rule across all zones
        r = session.delete('https://api.cloudflare.com/client/v4/user'
                           '/firewall/access_rules/rules/{}'.format(
                               rule_id))
    r.raise_for_status()
    res = r.json()
    if not res['success']:
        raise CloudFlareAPIError(res['errors']) 
Example 3
Project: ebay-watcher   Author: Snivyn   File: ebay-watcher.py    License: MIT License 6 votes vote down vote up
def __init__(self, product_link, domain):
        '''
        (str, str) -> eBae
        Given a link to an eBay product <product_link> and a catch-all domain
        address <domain>, a random email address is generated and an eBae
        object is returned.
        
        REQ: domain is a catch-all domain
        REQ: product_link is a link to a product listed on eBay
        '''
        self.s = requests.session()
        self.product_link = product_link
        self.s.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        }
        self.proxy_list = read_from_txt("proxies.txt")
        self.email = gen_email(domain) 
Example 4
Project: CyberTK-Self   Author: CyberTKR   File: Self.py    License: GNU General Public License v2.0 6 votes vote down vote up
def yt(query):
    with requests.session() as s:
         isi = []
         if query == "":
             query = "S1B tanysyz"   
         s.headers['user-agent'] = 'Mozilla/5.0'
         url    = 'http://www.youtube.com/results'
         params = {'search_query': query}
         r    = s.get(url, params=params)
         soup = BeautifulSoup(r.content, 'html5lib')
         for a in soup.select('.yt-lockup-title > a[title]'):
            if '&list=' not in a['href']:
                if 'watch?v' in a['href']:
                    b = a['href'].replace('watch?v=', '')
                    isi += ['youtu.be' + b]
         return isi 
Example 5
Project: pywren-ibm-cloud   Author: pywren   File: swift.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, swift_config, **kwargs):
        self.auth_url = swift_config['swift_auth_url']
        self.user_id = swift_config['swift_user_id']
        self.project_id = swift_config['swift_project_id']
        self.password = swift_config['swift_password']
        self.region = swift_config['swift_region']
        self.endpoint = None

        if 'token' in swift_config:
            self.token = swift_config['token']
            self.endpoint = swift_config['endpoint']
        else:
            self.token = self.generate_swift_token()
            swift_config['token'] = self.token
            swift_config['endpoint'] = self.endpoint

        self.session = requests.session()
        self.session.headers.update({'X-Auth-Token': self.token})
        adapter = requests.adapters.HTTPAdapter(pool_maxsize=64, max_retries=3)
        self.session.mount('http://', adapter)
        self.session.mount('https://', adapter) 
Example 6
Project: pywren-ibm-cloud   Author: pywren   File: swift.py    License: Apache License 2.0 6 votes vote down vote up
def put_object(self, container_name, key, data):
        """
        Put an object in Swift. Override the object if the key already exists.
        :param key: key of the object.
        :param data: data of the object
        :type data: str/bytes
        :return: None
        """
        url = '/'.join([self.endpoint, container_name, key])
        try:
            res = self.session.put(url, data=data)
            status = 'OK' if res.status_code == 201 else 'Error'
            try:
                logger.debug('PUT Object {} - Size: {} - {}'.format(key, sizeof_fmt(len(data)), status))
            except Exception:
                logger.debug('PUT Object {} - {}'.format(key, status))
        except Exception as e:
            print(e) 
Example 7
Project: pywren-ibm-cloud   Author: pywren   File: swift.py    License: Apache License 2.0 6 votes vote down vote up
def head_object(self, container_name, key):
        """
        Head object from Swift with a key. Throws StorageNoSuchKeyError if the given key does not exist.
        :param key: key of the object
        :return: Data of the object
        :rtype: str/bytes
        """
        url = '/'.join([self.endpoint, container_name, key])
        try:
            res = self.session.head(url)
            if res.status_code == 200:
                return res.headers
            elif res.status_code == 404:
                raise StorageNoSuchKeyError(container_name, key)
            else:
                raise Exception('{} - {}'.format(res.status_code, key))
        except Exception as e:
            raise StorageNoSuchKeyError(container_name, key) 
Example 8
Project: pywren-ibm-cloud   Author: pywren   File: swift.py    License: Apache License 2.0 6 votes vote down vote up
def bucket_exists(self, container_name):
        """
        Head container from Swift with a name. Throws StorageNoSuchKeyError if the given container does not exist.
        :param container_name: name of the container
        :return: Data of the bucket
        :rtype: str/bytes
        """
        url = '/'.join([self.endpoint, container_name])
        try:
            res = self.session.head(url)
            if res.status_code == 204:
                return res.headers
            elif res.status_code == 404:
                raise StorageNoSuchKeyError(container_name, '')
            else:
                raise Exception('{} - {}'.format(res.status_code))
        except Exception as e:
            raise StorageNoSuchKeyError(container_name, '') 
Example 9
Project: pywren-ibm-cloud   Author: pywren   File: swift.py    License: Apache License 2.0 6 votes vote down vote up
def list_objects(self, container_name, prefix=''):
        """
        Lists the objects in a bucket. Throws StorageNoSuchKeyError if the given bucket does not exist.
        :param key: key of the object
        :return: Data of the object
        :rtype: str/bytes
        """
        if prefix:
            url = '/'.join([self.endpoint, container_name, '?format=json&prefix='+prefix])
        else:
            url = '/'.join([self.endpoint, container_name, '?format=json'])
        try:
            res = self.session.get(url)
            objects = res.json()

            # TODO: Adapt to Key and Size
            return objects
        except Exception as e:
            raise e 
Example 10
Project: thewarden   Author: pxsocs   File: utils.py    License: MIT License 6 votes vote down vote up
def dojo_get_address(addr, at):
    # Request details about a collection of HD accounts and/or loose addresses and/or public keys.
    # Takes arguments:
    #   addr:   address or list of addresses
    #   at:     authentication token

    onion_address = dojo_get_settings()["onion"]

    url = "http://" + onion_address + "/v2/address/" + addr + "/info?at=" + at

    session = requests.session()
    session.proxies = {
        "http": "socks5h://127.0.0.1:9150",
        "https": "socks5h://127.0.0.1:9150",
    }
    try:
        auth_response = session.get(url)
    except requests.exceptions.ConnectionError:
        auth_response = {"status": "error", "error": "Connection Error"}
    return auth_response 
Example 11
Project: thewarden   Author: pxsocs   File: utils.py    License: MIT License 6 votes vote down vote up
def dojo_get_hd(xpub, at):
    # Request details about an HD account. If account does not exist, it must be created.
    # https://github.com/Samourai-Wallet/samourai-dojo/blob/master/doc/GET_xpub.md
    onion_address = dojo_get_settings()["onion"]
    url = "http://" + onion_address + "/v2/xpub/"
    session = requests.session()
    session.proxies = {
        "http": "socks5h://127.0.0.1:9150",
        "https": "socks5h://127.0.0.1:9150",
    }
    try:
        url = url + xpub + "?at=" + at
        auth_response = session.get(url)
    except requests.exceptions.ConnectionError:
        auth_response = {"status": "error", "error": "Connection Error"}
    return auth_response 
Example 12
Project: raveberry   Author: raveberry   File: youtube.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def youtube_session() -> Iterator[requests.Session]:
    """This context opens a requests session and loads the youtube cookies file."""
    session = requests.session()
    try:
        with open(
            os.path.join(settings.BASE_DIR, "config/youtube_cookies.pickle"), "rb"
        ) as f:
            session.cookies.update(pickle.load(f))
    except FileNotFoundError:
        pass

    headers = {
        "User-Agent": youtube_dl.utils.random_user_agent(),
    }
    session.headers.update(headers)
    yield session

    with open(
        os.path.join(settings.BASE_DIR, "config/youtube_cookies.pickle"), "wb"
    ) as f:
        pickle.dump(session.cookies, f) 
Example 13
Project: raveberry   Author: raveberry   File: youtube.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_search_suggestions(query: str) -> List[str]:
        """Returns a list of suggestions for the given query from Youtube."""
        with youtube_session() as session:
            params = {
                "client": "youtube",
                "q": query[:100],  # queries longer than 100 characters are not accepted
                "xhr": "t",  # this makes the response be a json file
            }
            response = session.get(
                "https://clients1.google.com/complete/search", params=params
            )
        suggestions = json.loads(response.text)
        # first entry is the query, the second one contains the suggestions
        suggestions = suggestions[1]
        # suggestions are given as tuples
        # extract the string and skip the query if it occurs identically
        suggestions = [entry[0] for entry in suggestions if entry[0] != query]
        return suggestions 
Example 14
Project: WizNote-to-OneNote   Author: CzBiX   File: onenote.py    License: Apache License 2.0 6 votes vote down vote up
def upload_doc(session, section_id, data_path, doc):
    doc_path = get_doc_path(data_path, doc)

    print('Processing %s%s (%s)' % (doc.location, doc.title, doc.guid))

    with ZipFile(doc_path) as zip_file:
        html_content, src_file_names = clean_html(zip_file.read('index.html'), doc)

        if len(src_file_names) > 5:
            print('Upload may failed if images more than 5')

        data_send = {
            'Presentation': (None, html_content, mimetypes.guess_type('index.html')[0])
        }

        for name in src_file_names:
            data_send[name] = (None, zip_file.read('index_files/' + name), mimetypes.guess_type(name)[0])

    resp = session.post(API_BASE + '/sections/%s/pages' % section_id, files=data_send)
    resp.raise_for_status() 
Example 15
Project: baidufm-py   Author: tdoly   File: fm_api.py    License: MIT License 6 votes vote down vote up
def _request(self, url, method=None, extra_params=None):
        params = dict()
        if extra_params:
            params.update(extra_params)

        headers = consts.HEADERS
        if 'fm.baidu.com' in url:
            headers['Host'] = "fm.baidu.com"
        elif 'passport.baidu.com' in url:
            headers['Host'] = "passport.baidu.com"
        else:
            headers['Host'] = ".baidu.com"

        self._params_utf8(params)
        if method and method.lower() == 'post':
            response = self.session.post(url, data=params, verify=True, headers=headers)
        else:
            if '?' in url:
                url = url + urlencode(params)
            else:
                url = url + '?' + urlencode(params)
            response = self.session.get(url, verify=False, headers=headers)
        return response 
Example 16
Project: redditDataExtractor   Author: NSchrading   File: imgurClientIdGUI.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(self):
        """
        A simple dialog for letting the user know that a client-id needs to be set to use album / gallery / page
        downloads from Imgur
        """
        QDialog.__init__(self)

        # Set up the user interface from Designer.
        self.setupUi(self)

        self.enterClientIdBtn.clicked.connect(self._checkClientIdLineEdit)
        self.enterLaterBtn.clicked.connect(self._enterLater)

        self._requestsSession = requests.session()
        self._requestsSession.headers[
            'User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'

        self.imgurAPIClientID = None 
Example 17
Project: redditDataExtractor   Author: NSchrading   File: redditDataExtractorGUI.py    License: GNU General Public License v3.0 6 votes vote down vote up
def viewRemainingImgurRequests(self):
        if self._rddtDataExtractor.currentlyDownloading:
            QMessageBox.warning(QMessageBox(), "Data Extractor for reddit",
                                "Cannot view imgur requests while currently downloading. Please wait.")
            return
        msgBox = QMessageBox()
        msgBox.setWindowTitle("Data Extractor for reddit")
        if self._rddtDataExtractor.imgurAPIClientID is not None:
            headers = {'Authorization': 'Client-ID ' + self._rddtDataExtractor.imgurAPIClientID}
            apiURL = "https://api.imgur.com/3/credits"
            requestsSession = requests.session()
            requestsSession.headers[
                'User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'
            json = exceptionSafeJsonRequest(requestsSession, apiURL, headers=headers, stream=True,
                                            verify='RedditDataExtractor/cacert.pem')
            if json is not None and json.get('data') is not None and json.get('data').get('ClientRemaining'):
                msgBox.setText("You have " + str(json.get('data').get('ClientRemaining')) + " requests remaining.")
            else:
                msgBox.setText(
                    "A problem occurred using the Imgur API. Check that you are connected to the internet and make sure your client-id is correct.")
        else:
            msgBox.setText(
                "You do not currently have an Imgur client-id set. To set one, go to settings and check 'Change / Reset Client-id'")
        msgBox.exec() 
Example 18
Project: cloudflare-tor-whitelister   Author: DonnchaC   File: whitelist.py    License: GNU General Public License v2.0 5 votes vote down vote up
def add_whitelist_rule(session, ip, zone_id=None):
    """
    Make request to CloudFlare API to whitelist an IP.
    """

    data = {
        "mode": "whitelist",
        "notes": "tor_exit",
        "configuration": {"value": ip, "target": "ip"},
    }

    # If zone_id, only apply rule to current zone/domain
    if zone_id:
        data.update({"group": {"id": "zone"}})
        r = session.post('https://api.cloudflare.com/client/v4/zones/{}'
                         '/firewall/access_rules/rules'.format(
                             zone_id), data=json.dumps(data))
    else:
        # Apply whitelist rules across all domains owned by this user.
        data.update({"group": {"id": "owner"}})
        r = session.post('https://api.cloudflare.com/client/v4/user'
                         '/firewall/access_rules/rules',
                         data=json.dumps(data))
    r.raise_for_status()
    res = r.json()
    if not res['success']:
        raise CloudFlareAPIError(res['errors']) 
Example 19
Project: CyberTK-Self   Author: CyberTKR   File: LineApi.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
    self.Talk = Talk()
    self._session = requests.session()
    self._headers = {'X-Line-Application': 'IOSIPAD\t7.14.0\tiPhone OS\t10.12.0', 'X-Line-Access': 'Emp1jl3qOjxCjXEhmaN5.QdLXoVPaKOU6WpvD80Sijq.NcwnmLOaI/dIyi3Y84WTCOxbNTN27m3ODDpkMLDPY64=', 'User-Agent': 'Line/7.14.0'} 
Example 20
Project: PickTrue   Author: winkidney   File: abstract.py    License: MIT License 5 votes vote down vote up
def __init__(self, proxies=None):
        self.session = requests.session()
        if proxies is not None:
            self.session.proxies = proxies
        self.session.headers.update(UA) 
Example 21
Project: PickTrue   Author: winkidney   File: abstract.py    License: MIT License 5 votes vote down vote up
def get(self, url, **kwargs):
        """
        :rtype: requests.Response
        """
        if 'timeout' in kwargs:
            kwargs.pop('timeout')
        return self.session.get(url, timeout=(2, 30), **kwargs) 
Example 22
Project: TaskBot   Author: EvilPsyCHo   File: tuling.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.session = requests.session()
        enhance_connection(self.session) 
Example 23
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, endpoint, namespace, api_key=None, auth=None, insecure=False, user_agent=None):
        """
        OpenWhiskClient Constructor

        :param endpoint: OpenWhisk endpoint.
        :param namespace: User namespace.
        :param api_key: User AUTH Key.  HTTP Basic authentication.
        :param auth: Authorization token string "Basic eyJraWQiOiIyMDE5MDcyNCIsImFsZ...".
        :param insecure: Insecure backend. Disable cert verification.
        :param user_agent: User agent on requests.
        """
        self.endpoint = endpoint.replace('http:', 'https:')
        self.namespace = namespace
        self.api_key = api_key
        self.auth = auth

        if self.api_key:
            api_key = str.encode(self.api_key)
            auth_token = base64.encodebytes(api_key).replace(b'\n', b'')
            self.auth = 'Basic %s' % auth_token.decode('UTF-8')

        self.session = requests.session()

        if insecure:
            self.session.verify = False

        self.headers = {
            'content-type': 'application/json',
            'Authorization': self.auth,
        }

        if user_agent:
            default_user_agent = self.session.headers['User-Agent']
            self.headers['User-Agent'] = default_user_agent + ' {}'.format(user_agent)

        self.session.headers.update(self.headers)
        adapter = requests.adapters.HTTPAdapter()
        self.session.mount('https://', adapter) 
Example 24
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def create_action(self, package, action_name, image_name, code=None, memory=None,
                      timeout=30000, kind='blackbox', is_binary=True, overwrite=True):
        """
        Create an IBM Cloud Functions action
        """
        data = {}
        limits = {}
        cfexec = {}
        limits['memory'] = memory
        limits['timeout'] = timeout
        data['limits'] = limits

        cfexec['kind'] = kind
        if kind == 'blackbox':
            cfexec['image'] = image_name
        cfexec['binary'] = is_binary
        cfexec['code'] = base64.b64encode(code).decode("utf-8") if is_binary else code
        data['exec'] = cfexec

        logger.debug('I am about to create a new cloud function action: {}'.format(action_name))
        url = '/'.join([self.endpoint, 'api', 'v1', 'namespaces', self.namespace, 'actions', package,
                        action_name + "?overwrite=" + str(overwrite)])

        res = self.session.put(url, json=data)
        resp_text = res.json()

        if res.status_code == 200:
            logger.debug("OK --> Created action {}".format(action_name))
        else:
            msg = 'An error occurred creating/updating action {}: {}'.format(action_name, resp_text['error'])
            raise Exception(msg) 
Example 25
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def get_action(self, package, action_name):
        """
        Get an IBM Cloud Functions action
        """
        logger.debug("I am about to get a cloud function action: {}".format(action_name))
        url = '/'.join([self.endpoint, 'api', 'v1', 'namespaces', self.namespace, 'actions', package, action_name])
        res = self.session.get(url)
        return res.json() 
Example 26
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def list_actions(self, package):
        """
        List all IBM Cloud Functions actions in a package
        """
        logger.debug("I am about to list all actions from: {}".format(package))
        url = '/'.join([self.endpoint, 'api', 'v1', 'namespaces', self.namespace, 'actions', package, ''])
        res = self.session.get(url)
        if res.status_code == 200:
            return res.json()
        else:
            return [] 
Example 27
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def delete_action(self, package, action_name):
        """
        Delete an IBM Cloud Function
        """
        logger.debug("Delete cloud function action: {}".format(action_name))
        url = '/'.join([self.endpoint, 'api', 'v1', 'namespaces', self.namespace, 'actions', package, action_name])
        res = self.session.delete(url)
        resp_text = res.json()

        if res.status_code != 200:
            logger.debug('An error occurred deleting action {}: {}'.format(action_name, resp_text['error'])) 
Example 28
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def list_packages(self):
        """
        List all IBM Cloud Functions packages
        """
        logger.debug('I am about to list all the IBM CF packages')
        url = '/'.join([self.endpoint, 'api', 'v1', 'namespaces', self.namespace, 'packages'])

        res = self.session.get(url)

        if res.status_code == 200:
            return res.json()
        else:
            logger.debug("Unable to list packages")
            raise Exception("Unable to list packages") 
Example 29
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def delete_package(self, package):
        """
        Delete an IBM Cloud Functions package
        """
        logger.debug("I am about to delete the package: {}".format(package))
        url = '/'.join([self.endpoint, 'api', 'v1', 'namespaces', self.namespace, 'packages', package])
        res = self.session.delete(url)
        resp_text = res.json()

        if res.status_code == 200:
            return resp_text
        else:
            logger.debug('An error occurred deleting the package {}: {}'.format(package, resp_text['error'])) 
Example 30
Project: pywren-ibm-cloud   Author: pywren   File: client.py    License: Apache License 2.0 5 votes vote down vote up
def create_package(self, package):
        """
        Create an IBM Cloud Functions package
        """
        logger.debug('I am about to create the package {}'.format(package))
        url = '/'.join([self.endpoint, 'api', 'v1', 'namespaces', self.namespace, 'packages', package + "?overwrite=False"])

        data = {"name": package}
        res = self.session.put(url, json=data)
        resp_text = res.json()

        if res.status_code != 200:
            logger.debug('Package {}: {}'.format(package, resp_text['error']))
        else:
            logger.debug("OK --> Created package {}".format(package))