• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python treq.request函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中treq.request函数的典型用法代码示例。如果您正苦于以下问题:Python request函数的具体用法?Python request怎么用?Python request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了request函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: _make_request

    def _make_request(self, method, path, data=None, *args, **kwargs):
        """Parse and create the request object."""
        data = json.dumps(data) if data is not None else None
        headers = self._all_extra_headers()
        new_headers = kwargs.pop("headers", {})
        headers.update(new_headers)

        if self.auth:
            kwargs['auth'] = self.auth

        if kwargs.get('params', {}):
            params = kwargs.get('params', {})

            for key, value in params.items():
                value = utf8(value) if isinstance(value, basestring) else value
                params[key] = value
            if params:
                kwargs['params'] = params

        return treq.request(
            method,
            path,
            headers=headers,
            data=data,
            **kwargs
        ).addCallbacks(
            self._handle_response,
            log.err
        )
开发者ID:dpnova,项目名称:eyeofthestorm,代码行数:29,代码来源:client.py


示例2: create

    def create(self, path='/'):
        self.data['address'] = str(self.data['address'])
        self.data['address_obj'] = urlparse(self.data['address'])

        # Build the necessary request headers for contacting the HTTP resource
        options = self.data['options']

        if 'auth_method' in options:
            if options['auth_method'].lower() == 'basic' and options['auth_username'] and options['auth_password']:
                self._treq_options['auth'] = (options['auth_username'], options['auth_password'])

        if 'timeout' in options:
            try:
                self._treq_options['timeout'] = int(options['timeout'])
            except:
                pass

        if 'user-agent' in options:
            self._treq_options['headers'] = {'User-Agent': [str(options['user-agent'])]}

        # Fetch the first page and determine which template parser is needed
        resp = yield request(method='GET', url=self._buildURL(path), **self._treq_options)
        if resp.code != 200:
            raise CrawlException('Response code was not 200: %s' % str(resp.code))

        self._resp_headers = dict((key.lower(), value[0].lower()) for (key, value) in resp.headers._rawHeaders.items())
        response_body = yield text_content(resp)

        self._indexParser = HttpParse().get_parser(response_headers=self._resp_headers,
                                                   response_body=response_body)

        if self._indexParser:
            defer.returnValue(True)

        defer.returnValue(False)
开发者ID:skftn,项目名称:findex-crawl,代码行数:35,代码来源:http.py


示例3: _query_mailchimp

def _query_mailchimp(data_center, method_section, method_name, payload):

    def _read_response(response):
        logger.info("Received Data: %s", response)
        if response.code != 200:
            logger.info("Received Data with error")
            raise MailChimpApiError("{}".format(response.phrase))

        return treq.text_content(response).addCallback(json.loads)

    def _check_for_error(response):
        logger.info("Received Data: %s", response)
        if "error" in response:
            raise MailChimpApiError("{}:{}".format(response["name"],
                                                   response["error"]))
        return response

    logger.info("STARTING QUERY MAICHIMP WITH PAYLOAD {}".format(payload))

    dfr = treq.request("POST",
                       MAILCHIMP_BASE_URL.format(dc=data_center,
                                                 section=method_section,
                                                 name=method_name).encode("utf-8"),
                       data=json.dumps(payload),
                       headers={"Content-Type": "application/json"})

    return dfr.addCallback(_read_response).addCallback(_check_for_error)
开发者ID:theGeoffrey,项目名称:theGeoffrey,代码行数:27,代码来源:mailchimp.py


示例4: _call

    def _call(self, method, call, **kwargs):
        url = self.endpoint + call
        headers = {'content-type': 'application/json',
                   'accept': 'application/json'}
        if self.session_id is not None:
            headers['coinsetter-client-session-id'] = self.session_id.encode('utf-8')
        new_kwargs = {'headers': headers}
        new_kwargs.update(kwargs)
        if 'data' in kwargs:
            new_kwargs['data'] = json.dumps(kwargs['data'])

        try:
            result = yield treq.request(method, url.encode('utf-8'), **new_kwargs)
        except (IOError, ConnectError) as e:
            log.err(e)
            self.emit("disconnect", self)
            raise e

        content = yield result.content()

        if result.code != 200:
            self.emit("disconnect", self)
            raise Exception("Error code %d received" % result.code)

        parsed = json.loads(content, parse_float=Decimal)
        returnValue(parsed)
开发者ID:Mrkebubun,项目名称:sputnik-old,代码行数:26,代码来源:coinsetter.py


示例5: request

    def request(self, method, url_suffix, parser=None, **kw):
        """
        Start the application, make an HTTP request and then shutdown
        the application.

        :param str url_suffix:
            A path to make the request to.
        :param str parser:
            Response parser to use. Valid values are ``'bytes'``, ``'json'``,
            ``'json_lines'`` or ``None``. ``None`` indicates that the raw
            response object should be returned. Otherwise the parsed data is
            returned.

        Other parameters are the same as for :func:`treq.request`.
        """
        server = self.reactor.listenTCP(0, self.app, interface="127.0.0.1")
        host = server.getHost()
        # prefix the URL with the test server host and port
        url = ('http://127.0.0.1:%d' % host.port) + url_suffix
        # close the HTTP connection straight away so that the test
        # reactor is clean when we leave this function
        kw['persistent'] = False
        response = yield treq.request(method, url, reactor=self.reactor, **kw)
        if parser is not None:
            parser_method = getattr(self, '_parse_' + parser)
            response = yield parser_method(response)
        yield server.stopListening()
        server.loseConnection()
        returnValue(response)
开发者ID:praekelt,项目名称:go-api-toolkit,代码行数:29,代码来源:helpers.py


示例6: process_queue

    def process_queue(self):
        while True:
            thing = yield self.queue_object.get()
            if thing is None:
                break
            ch, method, properties, body = thing
            if body:
                path = body
                print "PURGE %s %s" % (ua_map, path)
                try:
                    response = yield treq.request(
                        "PURGE", "http://127.0.0.1" + path,
                        cookies={"foo": "bar"},
                        headers={"Host": "actual.host.com"},
                        timeout=10
                    )
                except (ConnectError, DNSLookupError, CancelledError, ResponseFailed):
                    # Maybe better to do a blank except?
                    print "ERROR %s %s" (ua_map, path)
                else:
                    print "RESULT %s %s" % (ua_map, path)
                    content = yield response.content()
                    print content

            yield ch.basic_ack(delivery_tag=method.delivery_tag)
开发者ID:praekelt,项目名称:django-ultracache,代码行数:25,代码来源:monitor.py


示例7: _make_request

def _make_request(token, method_name, method='get', params=None, data=None, files=None, timeout=10, **kwargs):
  request_url = API_URL + 'bot' + token + '/' + method_name
  params = _convert_utf8(params)

  resp = yield treq.request(method, request_url, params=params, data=data, files=files, timeout=timeout, **kwargs)
  result_json = yield _check_response(resp, method_name)
  returnValue(result_json)
开发者ID:unintended,项目名称:twisted-telegram-bot,代码行数:7,代码来源:__init__.py


示例8: a_case

def a_case(fname):
    env = treq.load_py(os.path.splitext(fname)[0] + ".py")
    expect = env['request']
    cfg = env['cfg']
    req = treq.request(fname, expect)
    for case in req.gen_cases(cfg):
        case[0](*case[1:])
开发者ID:APAdmin,项目名称:gunicorn,代码行数:7,代码来源:test_001-valid-requests.py


示例9: _request

    def _request(self, http_method, url, payload=None):
        def _raise_error(txt):
            logger.info("ERROR: %s", txt)
            raise TwitterApiError(txt)

        def _print_response(response):
            logger.info("RESPONSE: ", response)
            return response

        def _read_response(response):
            if response.code != 200:
                return response.text().addCallback(_raise_error)

            return treq.content().addCallback(json.loads)

        headers = {}
        body = None
        client = self._oauth_client()

        if payload:
            body = urlencode(payload)
            headers = {'Content-Type': 'application/x-www-form-urlencoded'}

        xx, headers, body = client.sign(url, http_method=http_method,
                                        headers=headers, body=body)

        dfr = treq.request(http_method, url, headers=headers,
                           data=body)

        return dfr.addCallback(_read_response)
开发者ID:theGeoffrey,项目名称:theGeoffrey,代码行数:30,代码来源:twitter.py


示例10: _internal_call

    def _internal_call(self, method, url, payload, params):
        args = dict(params=params)
        if not url.startswith('http'):
            url = self.prefix + url
        headers = self._auth_headers()
        headers['Content-Type'] = ['application/json']

        if payload:
            args["data"] = json.dumps(payload)

        # TODO: treq can't handle unicode urls :(
        url = str(url)

        print("requesting %s" % url)
        r_d = treq.request(method, url, headers=Headers(headers), **args)

        r = yield r_d

        response_json = yield r.json()

        if self.trace:  # pragma: no cover
            print()
            print(method, url)
            if payload:
                print("DATA", json.dumps(payload))

        if r.code != 200:
            raise SpotifyException(r.code,
                                   -1, u'%s:\n %s' % (url, response_json['error']['message']))

        if self.trace:  # pragma: no cover
            print('RESP', response_json)
            print()
        defer.returnValue(response_json)
开发者ID:jimcortez,项目名称:spotipy_twisted,代码行数:34,代码来源:client.py


示例11: request

 def request(self, method, path, data=None):
     return treq.request(
         method, 'http://localhost:%s%s' % (
             self.listener_port,
             path
         ),
         data=data,
         pool=self.pool)
开发者ID:BantouTelecom,项目名称:portia,代码行数:8,代码来源:test_main.py


示例12: do_http

def do_http(method, url, **kwargs):
    response = yield treq.request(method, url, persistent=False, **kwargs)
    body = yield treq.content(response)
    # TODO: replace this with response.fail_for_status when
    # https://github.com/twisted/treq/pull/159 has landed
    if 400 <= response.code < 600:
        raise Error(response.code, response=body)
    defer.returnValue(body)
开发者ID:LeastAuthority,项目名称:tahoe-lafs,代码行数:8,代码来源:common_web.py


示例13: test_http_parser

def test_http_parser():
    for fname in glob.glob(os.path.join(reqdir, "*.http")):
        if os.getenv("GUNS_BLAZING"):
            expect = treq.load_py(os.path.splitext(fname)[0] + ".py")
            req = treq.request(fname, expect)
            for case in req.gen_cases():
                yield case
        else:
            yield (a_case, fname)
开发者ID:HubSpotForks,项目名称:gunicorn,代码行数:9,代码来源:001-test-valid-requests.py


示例14: assertHTTPError

 def assertHTTPError(self, url, code, response_substring,
                     method="get", persistent=False,
                     **args):
     response = yield treq.request(method, url, persistent=persistent,
                                   **args)
     body = yield response.content()
     self.assertEquals(response.code, code)
     if response_substring is not None:
         self.assertIn(response_substring, body)
     returnValue(body)
开发者ID:LeastAuthority,项目名称:tahoe-lafs,代码行数:10,代码来源:common.py


示例15: http_request

def http_request(url,method='GET',headers=None,params=None,data=None,callback=None,json=True, code_only=False):
  logging.critical('#####################################\n## THIS IS DEPRECATED - DO NOT USE ##\n#####################################')
  request = treq.request(url=url,method=method,headers=headers,data=data)
  if callback:
    if code_only:
      request.addCallback(callback)
    if json:
      request.addCallback(json_callback, callback)
    else:
      request.addCallback(text_callback, callback)
开发者ID:zpriddy,项目名称:Firefly,代码行数:10,代码来源:firefly_api.py


示例16: check_if_started

 def check_if_started():
     # Replace with ``GearClient.list`` as part of
     # https://github.com/hybridlogic/flocker/issues/32
     responded = request(
         b"GET", b"http://127.0.0.1:%d/containers" % (GEAR_PORT,),
         persistent=False)
     responded.addCallback(content)
     responded.addCallback(json.loads)
     responded.addCallback(is_started)
     return responded
开发者ID:lukemarsden,项目名称:flocker-old,代码行数:10,代码来源:test_gear.py


示例17: request

    def request(self, path, method="GET", headers=None, **kwargs):
        if headers is None:
            headers = Headers()

        headers.addRawHeader("User-Agent", self.user_agent)
        return request(
            method,
            headers=headers,
            url=self.root_url + path,
            params=[("fmt", "json")],
            reactor=self.reactor,
            **kwargs
        ).addCallback(json_content).addCallback(_check_for_errors)
开发者ID:Julian,项目名称:txmusicbrainz,代码行数:13,代码来源:client.py


示例18: request

    def request(self, method, uri, data=b'', headers=None):
        """
        Args:
            method (str): HTTP method to use.
            uri (str): URI to query.
            data (bytes): Data to send in the request body, if applicable.
            headers (t.w.http_headers.Headers): Request headers.

        Raises:
            SynapseError: If the IP is blacklisted.
        """
        # A small wrapper around self.agent.request() so we can easily attach
        # counters to it
        outgoing_requests_counter.labels(method).inc()

        # log request but strip `access_token` (AS requests for example include this)
        logger.info("Sending request %s %s", method, redact_uri(uri))

        try:
            request_deferred = treq.request(
                method,
                uri,
                agent=self.agent,
                data=data,
                headers=headers,
                **self._extra_treq_args
            )
            request_deferred = timeout_deferred(
                request_deferred,
                60,
                self.hs.get_reactor(),
                cancelled_to_request_timed_out_error,
            )
            response = yield make_deferred_yieldable(request_deferred)

            incoming_responses_counter.labels(method, response.code).inc()
            logger.info(
                "Received response to %s %s: %s", method, redact_uri(uri), response.code
            )
            defer.returnValue(response)
        except Exception as e:
            incoming_responses_counter.labels(method, "ERR").inc()
            logger.info(
                "Error sending request to  %s %s: %s %s",
                method,
                redact_uri(uri),
                type(e).__name__,
                e.args[0],
            )
            raise
开发者ID:DoubleMalt,项目名称:synapse,代码行数:50,代码来源:client.py


示例19: _request

    def _request(self, method, url, **kwargs):
        if 'headers' in kwargs:
            headers = self.config.base_headers.copy()
            headers.update(kwargs['headers'])
            kwargs['headers'] = headers
        else:
            kwargs['headers'] = self.config.base_headers

        if self.config.api_key_secret:
            kwargs['auth'] = (self.config.api_key_secret, '')

        d = treq.request(method, url, **kwargs)
        d.addCallback(self.deserialize)
        return d
开发者ID:trenton42,项目名称:txbalanced-v1.0,代码行数:14,代码来源:httpclient.py


示例20: perform_request_with_treq

def perform_request_with_treq(dispatcher, http_request):
    """A performer for :obj:`HTTPRequest` that uses the ``treq`` library."""
    headers = (
        http_request.headers.copy()
        if http_request.headers is not None
        else {})
    if 'user-agent' not in headers:
        headers['user-agent'] = ['Effect example']
    d = treq.request(
        http_request.method.lower(),
        http_request.url,
        headers=headers,
        data=http_request.data).addCallback(treq.content)
    return d
开发者ID:python-effect,项目名称:effect-examples,代码行数:14,代码来源:twisted_http.py



注:本文中的treq.request函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python client.HTTPClient类代码示例发布时间:2022-05-27
下一篇:
Python treq.post函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap