Python requests.Request() Examples

The following are 30 code examples of requests.Request(). 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: Telerik CVE-2017-9248.py    From PayloadsAllTheThings with 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 #2
Source File: comm.py    From Paradrop with 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
Source File: zmirror.py    From zmirror with 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 #4
Source File: extending.py    From tekore with 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 #5
Source File: extending.py    From tekore with 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 #6
Source File: extending.py    From tekore with 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 #7
Source File: extending.py    From tekore with 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 #8
Source File: extending.py    From tekore with 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 #9
Source File: expiring.py    From tekore with 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 #10
Source File: expiring.py    From tekore with 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 #11
Source File: expiring.py    From tekore with 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 #12
Source File: expiring.py    From tekore with 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 #13
Source File: modify.py    From tekore with 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 #14
Source File: models.py    From schemathesis with 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 #15
Source File: JenkinsWrapper.py    From ngraph-onnx with 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 #16
Source File: oauth2.py    From courseraprogramming with 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 #17
Source File: models.py    From schemathesis with MIT License 5 votes vote down vote up
def from_wsgi(cls, case: Case, response: WSGIResponse, headers: Dict[str, Any], elapsed: float) -> "Interaction":
        session = requests.Session()
        session.headers.update(headers)
        return cls(request=Request.from_case(case, session), response=Response.from_wsgi(response, elapsed)) 
Example #18
Source File: models.py    From schemathesis with MIT License 5 votes vote down vote up
def from_requests(cls, response: requests.Response) -> "Interaction":
        return cls(request=Request.from_prepared_request(response.request), response=Response.from_requests(response)) 
Example #19
Source File: retrying.py    From tekore with MIT License 5 votes vote down vote up
def test_rate_limited_retry_doesnt_decrease_retry_count(self):
        fail = failed_response()
        rate = rate_limit_response()
        success = ok_response()
        sender = mock_sender(fail, rate, fail, success)

        s = RetryingSender(retries=2, sender=sender)
        with patch(module + '.time', MagicMock()):
            s.send(Request())

        assert sender.send.call_count == 4 
Example #20
Source File: client.py    From tekore with MIT License 5 votes vote down vote up
def _send(self, request: Request) -> Response:
        return self.sender.send(request) 
Example #21
Source File: retrying.py    From tekore with MIT License 5 votes vote down vote up
def test_retry_returns_on_first_success(self):
        fail = failed_response()
        success = ok_response()
        sender = mock_sender(fail, fail, success, fail, success)

        s = RetryingSender(retries=5, sender=sender)
        with patch(module + '.time', MagicMock()):
            s.send(Request())
        assert sender.send.call_count == 3 
Example #22
Source File: test_auth.py    From poetry with MIT License 5 votes vote down vote up
def test_auth_with_request_with_same_authentication():
    auth = Auth("https://python-poetry.org", "foo", "bar")

    request = Request("GET", "https://foo:bar@python-poetry.org/docs/")
    assert "Authorization" not in request.headers

    request = auth(request)

    assert "Authorization" in request.headers
    assert request.headers["Authorization"] == "Basic {}".format(
        decode(base64.b64encode(encode(":".join(("foo", "bar")))))
    ) 
Example #23
Source File: models.py    From schemathesis with MIT License 5 votes vote down vote up
def get_full_url(self) -> str:
        """Make a full URL to the current endpoint, including query parameters."""
        base_url = self.base_url or "http://localhost"
        kwargs = self.as_requests_kwargs(base_url)
        request = requests.Request(**kwargs)
        prepared = requests.Session().prepare_request(request)  # type: ignore
        return prepared.url 
Example #24
Source File: commons.py    From SalesforcePy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request(self):
        """ Makes request to Salesforce and returns serialised response. Catches any exceptions and appends them to
        `self.exceptions`.

          :return: response: Salesforce response, if available
          :rtype: list|dict|None
        """
        (headers, logger, request_object, response, service) = self.get_request_vars()
        logging.getLogger('sfdc_py').info('%s %s' %
                                          (self.http_method, service))

        if self.http_method == 'POST':
            request_fn = post_request
        elif self.http_method == 'PUT':
            request_fn = put_request
        elif self.http_method == 'PATCH':
            request_fn = patch_request
        elif self.http_method == 'DELETE':
            request_fn = delete_request
        else:
            request_fn = get_request

        try:
            request_object = request_fn(self)
            self.status = request_object.status_code

            if request_object.content.decode('utf-8') == 'null':
                raise SFDCRequestException('Request body is null')
            else:
                response = request_object.json()
        except Exception as e:
            self.exceptions.append(e)
            logger.error('%s %s %s' % (self.http_method, service, self.status))
            logger.error(e.message)
            return
        finally:
            return response 
Example #25
Source File: connection.py    From insightconnect-plugins with MIT License 5 votes vote down vote up
def __init__(self):
        super(self.__class__, self).__init__(input=ConnectionSchema())
        self.session = Session()
        self.request = Request() 
Example #26
Source File: CollectionJenkins.py    From FXTest with MIT License 5 votes vote down vote up
def job_bulid_log(self,url,jobname):
        id=self.servir.get_job_info(jobname)['lastCompletedBuild']['number']
        url1=url+str(id)+"/console"
        log=self.servir.jenkins_request(requests.Request('GET',url1)).text
        return log 
Example #27
Source File: zmirror.py    From zmirror with 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 #28
Source File: auth.py    From openeo-python-client with Apache License 2.0 5 votes vote down vote up
def __call__(self, req: Request) -> Request:
        # Do nothing by default
        return req 
Example #29
Source File: auth.py    From openeo-python-client with Apache License 2.0 5 votes vote down vote up
def __call__(self, req: Request) -> Request:
        # Add bearer authorization header.
        req.headers['Authorization'] = "Bearer {b}".format(b=self.bearer)
        return req 
Example #30
Source File: commons.py    From SalesforcePy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_request_vars(self):
        """ Returns the variables required by `request()` and other functions.

          :return: (headers, logger, request_object, response, service)
          :rtype: (dict, logging.Logger, requests.Request|None, list|dict|None, string)
        """
        return (
            self.get_headers(),
            logging.getLogger('sfdc_py'),
            None,
            None,
            self.get_request_url()
        )