Python requests.Request() Examples

The following are 30 code examples for showing how to use requests.Request(). 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: zmirror   Author: aploium   File: zmirror.py    License: MIT License 6 votes vote down vote up
def extract_url_path_and_query(full_url=None, no_query=False):
    """
    Convert http://foo.bar.com/aaa/p.html?x=y to /aaa/p.html?x=y

    :param no_query:
    :type full_url: str
    :param full_url: full url
    :return: str
    """
    if full_url is None:
        full_url = request.url
    split = urlsplit(full_url)
    result = split.path or "/"
    if not no_query and split.query:
        result += '?' + split.query
    return result


# ################# End Client Request Handler #################


# ################# Begin Middle Functions ################# 
Example 2
Project: Paradrop   Author: ParadropLabs   File: comm.py    License: Apache License 2.0 6 votes vote down vote up
def send_router_login(request_url, username, password, session):
    url_parts = urlparse(request_url)

    auth_url = "{}://{}/api/v1/auth/local".format(url_parts.scheme, url_parts.netloc)
    data = {
        'username': username,
        'password': password
    }
    request = requests.Request('POST', auth_url, json=data)
    prepped = session.prepare_request(request)
    res = session.send(prepped)
    print("Server responded: {} {}".format(res.status_code, res.reason))

    if res.ok:
        data = res.json()
        token = data['token']

        return (res.status_code, token)

    else:
        return (res.status_code, None) 
Example 3
Project: tekore   Author: felix-hilden   File: extending.py    License: MIT License 6 votes vote down vote up
def send(self, request: Request) -> Response:
        """Delegate request to underlying sender and retry if failed."""
        if self.is_async:
            return self._async_send(request)

        tries = self.retries + 1
        delay_seconds = 1

        while tries > 0:
            r = self.sender.send(request)

            if r.status_code == 429:
                seconds = r.headers.get('Retry-After', 1)
                time.sleep(int(seconds) + 1)
            elif r.status_code >= 500 and tries > 1:
                tries -= 1
                time.sleep(delay_seconds)
                delay_seconds *= 2
            else:
                return r 
Example 4
Project: tekore   Author: felix-hilden   File: extending.py    License: MIT License 6 votes vote down vote up
def _async_send(self, request: Request) -> Response:
        tries = self.retries + 1
        delay_seconds = 1

        while tries > 0:
            r = await self.sender.send(request)

            if r.status_code == 429:
                seconds = r.headers.get('Retry-After', 1)
                await asyncio.sleep(int(seconds) + 1)
            elif r.status_code >= 500 and tries > 1:
                tries -= 1
                await asyncio.sleep(delay_seconds)
                delay_seconds *= 2
            else:
                return r 
Example 5
Project: tekore   Author: felix-hilden   File: extending.py    License: MIT License 6 votes vote down vote up
def _load(self, request: Request) -> tuple:
        params = ('&' + urlencode(request.params)) if request.params else ''
        url = request.url + params
        item = self._cache.get(url, None)

        if item is None:
            return None, None

        vary_key = self._vary_key(request, item[0])
        cached = item[1].get(vary_key, None)

        if cached is not None:
            response = cached['response']
            deque_item = (url, vary_key)
            if self._cc_fresh(cached):
                self._update_usage(deque_item)
                return response, None
            elif self._has_etag(cached):
                self._update_usage(deque_item)
                return response, cached['etag']
            elif self.max_size is not None:
                self._deque.remove(deque_item)

        return None, None 
Example 6
Project: tekore   Author: felix-hilden   File: extending.py    License: MIT License 6 votes vote down vote up
def send(self, request: Request) -> Response:
        """Maybe load request from cache, or delegate to underlying sender."""
        if self.is_async:
            return self._async_send(request)

        if request.method.lower() != 'get':
            return self.sender.send(request)

        cached, etag = self._load(request)
        if cached is not None and etag is None:
            return cached
        elif etag is not None:
            request.headers.update(ETag=etag)

        fresh = self.sender.send(request)
        return self._handle_fresh(request, fresh, cached) 
Example 7
Project: tekore   Author: felix-hilden   File: extending.py    License: MIT License 6 votes vote down vote up
def _async_send(self, request: Request):
        if request.method.lower() != 'get':
            return await self.sender.send(request)

        if self._lock is None:
            self._lock = asyncio.Lock()

        async with self._lock:
            cached, etag = self._load(request)

        if cached is not None and etag is None:
            return cached
        elif etag is not None:
            request.headers.update(ETag=etag)

        fresh = await self.sender.send(request)
        async with self._lock:
            return self._handle_fresh(request, fresh, cached) 
Example 8
Project: tekore   Author: felix-hilden   File: expiring.py    License: MIT License 6 votes vote down vote up
def send_and_process_token(
        function: Callable[..., Request]
) -> Callable[..., Token]:
    """Send request and parse reponse for token."""
    async def async_send(self, request: Request):
        response = await self._send(request)
        return parse_token(response)

    @wraps(function)
    def wrapper(self, *args, **kwargs):
        request = function(self, *args, **kwargs)

        if self.is_async:
            return async_send(self, request)

        response = self._send(request)
        return parse_token(response)
    return wrapper 
Example 9
Project: tekore   Author: felix-hilden   File: expiring.py    License: MIT License 6 votes vote down vote up
def send_and_process_refreshed_token(
        function: Callable[..., Request]
) -> Callable[..., Token]:
    """Send request and parse refreshed token."""
    async def async_send(self, request: Request, refresh_token: str):
        response = await self._send(request)
        return parse_refreshed_token(response, refresh_token)

    @wraps(function)
    def wrapper(self, *args, **kwargs):
        request, refresh_token = function(self, *args, **kwargs)

        if self.is_async:
            return async_send(self, request, refresh_token)

        response = self._send(request)
        return parse_refreshed_token(response, refresh_token)
    return wrapper 
Example 10
Project: tekore   Author: felix-hilden   File: expiring.py    License: MIT License 6 votes vote down vote up
def request_user_token(self, code: str) -> Token:
        """
        Request a new user token.

        Step 2/2 in authorisation code flow.
        Code is provided as a URL parameter in the redirect URI
        after login in step 1.

        Parameters
        ----------
        code
            code from redirect parameters

        Returns
        -------
        Token
            user access token
        """
        payload = {
            'code': code,
            'redirect_uri': self.redirect_uri,
            'grant_type': 'authorization_code'
        }
        return self._request_token(payload) 
Example 11
Project: tekore   Author: felix-hilden   File: expiring.py    License: MIT License 6 votes vote down vote up
def refresh_user_token(self, refresh_token: str) -> Token:
        """
        Request a refreshed user token.

        Parameters
        ----------
        refresh_token
            refresh token

        Returns
        -------
        Token
            refreshed user access token
        """
        payload = {
            'refresh_token': refresh_token,
            'grant_type': 'refresh_token'
        }

        return self._request_token(payload), refresh_token 
Example 12
Project: tekore   Author: felix-hilden   File: modify.py    License: MIT License 6 votes vote down vote up
def playlist_cover_image_upload(self, playlist_id: str, image: str) -> None:
        """
        Upload a custom playlist cover image.

        Parameters
        ----------
        playlist_id
            playlist ID
        image
            image data as a base64-encoded string
        """
        return Request(
            method='PUT',
            url=build_url(f'playlists/{playlist_id}/images'),
            headers=self._create_headers(content_type='image/jpeg'),
            data=image
        ) 
Example 13
Project: schemathesis   Author: kiwicom   File: models.py    License: MIT License 6 votes vote down vote up
def from_prepared_request(cls, prepared: requests.PreparedRequest) -> "Request":
        """A prepared request version is already stored in `requests.Response`."""
        body = prepared.body or b""

        if isinstance(body, str):
            # can be a string for `application/x-www-form-urlencoded`
            body = body.encode("utf-8")

        # these values have `str` type at this point
        uri = cast(str, prepared.url)
        method = cast(str, prepared.method)
        return cls(
            uri=uri,
            method=method,
            headers={key: [value] for (key, value) in prepared.headers.items()},
            body=base64.b64encode(body).decode(),
        ) 
Example 14
Project: ngraph-onnx   Author: NervanaSystems   File: JenkinsWrapper.py    License: Apache License 2.0 6 votes vote down vote up
def get_idle_ci_hosts(self):
        """Query Jenkins for idle servers.

        Send GET request to Jenkins server, querying for idle servers labeled
        for nGraph-ONNX CI job.

            :return:     Number of idle hosts delegated to nGraph-ONNX CI
            :rtype:      int
        """
        jenkins_request_url = self.jenkins_server + 'label/ci&&onnx/api/json?pretty=true'
        try:
            log.info('Sending request to Jenkins: %s', jenkins_request_url)
            r = requests.Request(method='GET', url=jenkins_request_url, verify=False)
            response = self.jenkins.jenkins_request(r).json()
            return int(response['totalExecutors']) - int(response['busyExecutors'])
        except Exception as e:
            log.exception('Failed to send request to Jenkins!\nException message: %s', str(e))
            raise 
Example 15
Project: courseraprogramming   Author: coursera   File: oauth2.py    License: Apache License 2.0 6 votes vote down vote up
def _build_authorizaton_url(self, state_token):
        authorization_request = requests.Request(
            'GET',
            self.auth_endpoint,
            params={
                'access_type': 'offline',
                'response_type': 'code',
                'client_id': self.client_id,
                'redirect_uri': self._redirect_uri,
                'scope': self.scopes,
                'state': state_token,
            }).prepare()

        logging.debug('Constructed authoriation request at: %s',
                      authorization_request.url)
        return authorization_request.url 
Example 16
Project: PayloadsAllTheThings   Author: swisskyrepo   File: Telerik CVE-2017-9248.py    License: MIT License 6 votes vote down vote up
def get_result(plaintext, key, session, pad_chars):
    global requests_sent, char_requests

    url = args.url
    base_pad = (len(key) % 4)
    base = '' if base_pad == 0 else pad_chars[0:4 - base_pad]
    dp_encrypted = base64.b64encode(
                                (encrypt(plaintext, key) + base).encode()
                            ).decode()
    request = requests.Request('GET', url + '?dp=' + dp_encrypted)
    request = request.prepare()
    response = session.send(request, verify=False, proxies = getProxy(args.proxy))
    requests_sent += 1
    char_requests += 1

    match = re.search("(Error Message:)(.+\n*.+)(</div>)", response.text)
    return True \
        if match is not None \
        and match.group(2) == args.oracle \
        else False 
Example 17
Project: zmirror   Author: aploium   File: zmirror.py    License: MIT License 5 votes vote down vote up
def response_cookie_rewrite(cookie_string):
    """
    rewrite response cookie string's domain to `my_host_name`
    :type cookie_string: str
    """
    cookie_string = regex_cookie_rewriter.sub('domain=' + my_host_name_no_port, cookie_string)
    return cookie_string


# ################# End Server Response Handler #################


# ################# Begin Client Request Handler ################# 
Example 18
Project: zmirror   Author: aploium   File: zmirror.py    License: MIT License 5 votes vote down vote up
def filter_client_request():
    """过滤用户请求, 视情况拒绝用户的访问
    :rtype: Union[Response, None]
    """
    dbgprint('Client Request Url: ', request.url)

    # crossdomain.xml
    if os.path.basename(request.path) == 'crossdomain.xml':
        dbgprint('crossdomain.xml hit from', request.url)
        return crossdomain_xml()

    # Global whitelist ua
    if check_global_ua_pass(str(request.user_agent)):
        return None

    if is_deny_spiders_by_403 and is_denied_because_of_spider(str(request.user_agent)):
        return generate_simple_resp_page(b'Spiders Are Not Allowed To This Site', 403)

    if human_ip_verification_enabled and (
                ((human_ip_verification_whitelist_from_cookies or enable_custom_access_cookie_generate_and_verify)
                 and must_verify_cookies)
            or is_ip_not_in_allow_range(request.remote_addr)
    ):
        dbgprint('ip', request.remote_addr, 'is verifying cookies')
        if 'zmirror_verify' in request.cookies and \
                ((human_ip_verification_whitelist_from_cookies and verify_ip_hash_cookie(request.cookies.get('zmirror_verify')))
                 or (enable_custom_access_cookie_generate_and_verify and custom_verify_access_cookie(
                        request.cookies.get('zmirror_verify'), request))):
            ip_whitelist_add(request.remote_addr, info_record_dict=request.cookies.get('zmirror_verify'))
            dbgprint('add to ip_whitelist because cookies:', request.remote_addr)
        else:
            return redirect(
                "/ip_ban_verify_page?origin=" + base64.urlsafe_b64encode(str(request.url).encode(encoding='utf-8')).decode(
                    encoding='utf-8'),
                code=302)

    return None 
Example 19
Project: botbuilder-python   Author: microsoft   File: language_helper.py    License: MIT License 5 votes vote down vote up
def call_model_runtime(
        configuration: Dict[str, object], text: str
    ) -> requests.Request:
        """ Makes a call to the model runtime api

        The model runtime api signature is:
          http://<model_runtime_host>:<port>/v1.0/model?q=<text>

        where:

          model_runtime_host - The host running the model runtime api.  To resolve
            the host running the model runtime api (in the following order):
            - MODEL_RUNTIME_API environment variable.  Used in docker.
            - config.py (which contains the DefaultConfig class).  Used running
                locally.

          port - http port number (ie, 8880)

          q - A query string to process (ie, the text utterance from user)

        For more details: (See TBD swagger file)
        """
        port = os.environ.get("MODEL_RUNTIME_SERVICE_PORT")
        host = os.environ.get("MODEL_RUNTIME_SERVICE_HOST")
        if host is None:
            host = configuration["MODEL_RUNTIME_SERVICE_HOST"]
        if port is None:
            port = configuration["MODEL_RUNTIME_SERVICE_PORT"]

        api_url = f"http://{host}:{port}/v1.0/model"
        qstrings = {"q": text}
        return requests.get(api_url, params=qstrings) 
Example 20
Project: tekore   Author: felix-hilden   File: concrete.py    License: MIT License 5 votes vote down vote up
def send(self, request: Request) -> Response:
        """
        Prepare and send a request.

        Parameters
        ----------
        request
            :class:`Request` to send
        """ 
Example 21
Project: tekore   Author: felix-hilden   File: concrete.py    License: MIT License 5 votes vote down vote up
def send(self, request: Request) -> Response:
        """Send request with new session."""
        with Session() as sess:
            prepared = sess.prepare_request(request)
            return sess.send(prepared, **self.requests_kwargs) 
Example 22
Project: tekore   Author: felix-hilden   File: concrete.py    License: MIT License 5 votes vote down vote up
def send(self, request: Request) -> Response:
        """Send request with new client."""
        async with AsyncClient() as client:
            return await client.request(
                request.method,
                request.url,
                data=request.data or None,
                params=request.params or None,
                headers=request.headers,
                **self.httpx_kwargs,
            ) 
Example 23
Project: tekore   Author: felix-hilden   File: concrete.py    License: MIT License 5 votes vote down vote up
def send(self, request: Request) -> Response:
        """Send request with global session."""
        prepared = SingletonSender.session.prepare_request(request)
        return SingletonSender.session.send(prepared, **self.requests_kwargs) 
Example 24
Project: tekore   Author: felix-hilden   File: concrete.py    License: MIT License 5 votes vote down vote up
def send(self, request: Request) -> Response:
        """Send request with global client."""
        return await AsyncSingletonSender.client.request(
            request.method,
            request.url,
            data=request.data or None,
            params=request.params or None,
            headers=request.headers,
            **self.httpx_kwargs,
        ) 
Example 25
Project: tekore   Author: felix-hilden   File: concrete.py    License: MIT License 5 votes vote down vote up
def send(self, request: Request) -> Response:
        """Send request with instance client."""
        return await self.client.request(
            request.method,
            request.url,
            data=request.data or None,
            params=request.params or None,
            headers=request.headers,
            **self.httpx_kwargs,
        ) 
Example 26
Project: tekore   Author: felix-hilden   File: client.py    License: MIT License 5 votes vote down vote up
def _send(self, request: Request) -> Response:
        return self.sender.send(request) 
Example 27
Project: tekore   Author: felix-hilden   File: extending.py    License: MIT License 5 votes vote down vote up
def _vary_key(request: Request, vary: Optional[list]):
        if vary is not None:
            return ' '.join(request.headers[k] for k in vary) 
Example 28
Project: tekore   Author: felix-hilden   File: expiring.py    License: MIT License 5 votes vote down vote up
def request_client_token(self) -> Token:
        """
        Request a client token.

        Returns
        -------
        Token
            client access token
        """
        payload = {'grant_type': 'client_credentials'}
        return self._request_token(payload) 
Example 29
Project: tekore   Author: felix-hilden   File: base.py    License: MIT License 5 votes vote down vote up
def _request(
            self,
            method: str,
            url: str,
            payload=None,
            params: dict = None
    ):
        return Request(
            method=method,
            url=build_url(url),
            headers=self._create_headers(),
            params=parse_url_params(params),
            data=json.dumps(payload) if payload is not None else None
        ) 
Example 30
Project: tekore   Author: felix-hilden   File: handle.py    License: MIT License 5 votes vote down vote up
def handle_errors(request: Request, response: Response) -> None:
    """Examine response and raise errors accordingly."""
    if response.status_code >= 400:
        error_str = error_format.format(
            url=response.url,
            code=response.status_code,
            msg=parse_error_reason(response)
        )
        error_cls = get_error(response.status_code)
        raise error_cls(error_str, request=request, response=response)