当前位置: 首页>>代码示例>>Python>>正文


Python HTTPRequest.request_timeout方法代码示例

本文整理汇总了Python中tornado.httpclient.HTTPRequest.request_timeout方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPRequest.request_timeout方法的具体用法?Python HTTPRequest.request_timeout怎么用?Python HTTPRequest.request_timeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tornado.httpclient.HTTPRequest的用法示例。


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

示例1: perform_request

# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import request_timeout [as 别名]
    def perform_request(self, request, response, method):
        try:
            constants = request_consts[method]

            url = request[constants.URL]
            timeout = request[constants.TIMEOUT]

            http_request = HTTPRequest(url=url, method=method)
            http_request.request_timeout = float(timeout)/1000

            if method == 'POST':
                http_request.body = request[constants.BODY]

            #adds cookies to request
            params_num = len(request)
            if constants.COOKIES <= params_num - 1:
                cookies = request[constants.COOKIES]
                if len(cookies) > 0:
                    list_of_cookies = list('{0}={1}'.format(cookie, value) for cookie, value in cookies.iteritems())
                    cookies_str = '; '.join(list_of_cookies)

                    http_request.headers.add('Cookie', cookies_str)

            #adds headers to request
            if constants.HEADERS <= params_num - 1:
                for name, values_list in request[constants.HEADERS].iteritems():
                    for value in values_list:
                        http_request.headers.add(name, value)

            self.logger.info("Downloading {0}, headers {1}, method {2}".format(url, http_request.headers, method))
            http_response = yield self.http_client.fetch(http_request)

            response_headers = self._get_headers_from_response(http_response)
            response.write((True, http_response.body, http_response.code, response_headers,))

            response.close()
            self.logger.info("{0} has been successfuly downloaded".format(url))
        except HTTPError as e:
            self.logger.info("Error ({0}) occured while downloading {1}".format(e.message, url))

            if e.response is not None:
                http_response = e.response
                response_headers = self._get_headers_from_response(http_response)
                response.write((False, http_response.body, http_response.code, response_headers,))
            else:
                response.write((False, '', e.code, {},))

            response.close()
        except socket.gaierror as e:
            self.logger.info("Error ({0}) occured while downloading {1}".format(e.message, url))
            response.write((False, '', e.errno, {},))
            response.close()
        except Exception as e:
            self.logger.error("Unhandled error ({0}) occured in perform_request, report about this problem "
                          "to httpclient service developers. Method is {1}, stacktrace is: {2}".format(
                                e.message, method, traceback.format_exc()))

            response.write((False, '', 0, {},))
            response.close()
开发者ID:lamerman,项目名称:httpclient-service,代码行数:61,代码来源:pyurlfetcher.py

示例2: create_request

# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import request_timeout [as 别名]
 def create_request(self):
     request = HTTPRequest(url=self.request_data['url'], headers=self.request_data['headers'])
     self.patch_request(request)
     request.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36'
     #request.connect_timeout = 0
     request.request_timeout = 0
     request.header_only = False
     return request
开发者ID:mljack,项目名称:xunlei-lixian-proxy,代码行数:10,代码来源:tornado_httpproxyclient.py

示例3: fetch

# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import request_timeout [as 别名]
 def fetch(self, path, **kwargs):
     kwargs['url'] = self.get_url(path)
     body = kwargs.pop('body', None)
     request = HTTPRequest(**kwargs)
     request.body = body
     request.allow_nonstandard_methods = True
     request.request_timeout = TIMEOUT
     self.http_client.fetch(request, self.stop, **kwargs)
     return self.wait()
开发者ID:bmentges,项目名称:brainiak_api,代码行数:11,代码来源:tornado_cases.py

示例4: request

# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import request_timeout [as 别名]
    def request(self, method, url, query_params=None, headers=None,
                      body=None, post_params=None, _preload_content=True, _request_timeout=None):
        """
        :param method: http request method
        :param url: http request url
        :param query_params: query parameters in the url
        :param headers: http request headers
        :param body: request json body, for `application/json`
        :param post_params: request post parameters,
                            `application/x-www-form-urlencoded`
                            and `multipart/form-data`
        :param _preload_content: this is a non-applicable field for the AiohttpClient.
        :param _request_timeout: timeout setting for this request. If one number provided, it will be total request
                                 timeout. It can also be a pair (tuple) of (connection, read) timeouts.
        """
        method = method.upper()
        assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH', 'OPTIONS']

        if post_params and body:
            raise ValueError(
                "body parameter cannot be used with post_params parameter."
            )

        request = HTTPRequest(url)
        request.ssl_context = self.ssl_context
        request.proxy_host = self.proxy_host
        request.proxy_port = self.proxy_port
        request.method = method
        if headers:
            request.headers = headers
        if 'Content-Type' not in headers:
            request.headers['Content-Type'] = 'application/json'
        request.request_timeout = _request_timeout or 5 * 60


        post_params = post_params or {}

        if query_params:
            request.url += '?' + urlencode(query_params)

        # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
        if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
            if re.search('json', headers['Content-Type'], re.IGNORECASE):
                if body:
                    body = json.dumps(body)
                request.body = body
            elif headers['Content-Type'] == 'application/x-www-form-urlencoded':
                request.body = urlencode(post_params)
            # TODO: transform to multipart form
            elif headers['Content-Type'] == 'multipart/form-data':
                request.body = encode_multipart_formdata(post_params)
            # Pass a `bytes` parameter directly in the body to support
            # other content types than Json when `body` argument is provided
            # in serialized form
            elif isinstance(body, bytes):
                request.body = body
            else:
                # Cannot generate the request from given parameters
                msg = """Cannot prepare a request message for provided arguments.
                Please check that your arguments match declared content type."""
                raise ApiException(status=0, reason=msg)

        r = yield self.pool_manager.fetch(request)
        r = RESTResponse(r, r.body)

        # log response body
        logger.debug("response body: %s", r.data)

        if not 200 <= r.status <= 299:
            raise ApiException(http_resp=r)

        return r
开发者ID:markcuk,项目名称:swagger-codegen,代码行数:74,代码来源:rest.py


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