sessions.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:trip 作者: littlecodersh 项目源码 文件源码
def send(self, req, **kwargs):
        """Send a given PreparedRequest.

        :rtype: trip.gen.Future
        """

        if not isinstance(req, PreparedRequest):
            raise ValueError('You can only send PreparedRequests.')

        allow_redirects = kwargs.pop('allow_redirects', True)
        start_time = preferred_clock()

        r = yield self.adapter.send(req, **kwargs)

        if isinstance(r, Exception):
            raise gen.Return(r)
        else:
            r = self.prepare_response(req, r)

        r.elapsed = timedelta(seconds=(preferred_clock()-start_time))

        # Response manipulation hooks
        r = dispatch_hook('response', req.hooks, r, **kwargs)

        # Persist cookies
        if r.history:
            # If the hooks create history then we want those cookies too
            for resp in r.history:
                self.cookies.extract_cookies(
                    MockResponse(HTTPHeaderDict(resp.headers)), MockRequest(resp.request))

        self.cookies.extract_cookies(
            MockResponse(HTTPHeaderDict(r.headers)), MockRequest(req))

        # Redirect resolving generator.
        redirect_gen = self.resolve_redirects(r, req, **kwargs)

        # Resolve redirects if allowed.
        history = []
        if allow_redirects:
            for resp in redirect_gen:
                resp = yield resp
                history.append(resp)

        # Shuffle things around if there's history.
        if history:
            # Insert the first (original) request at the start
            history.insert(0, r)
            # Get the last request made
            r = history.pop()
            r.history = history

        # If redirects aren't being followed, store the response on the Request for Response.next().
        if not allow_redirects:
            try:
                r._next = next(self.resolve_redirects(r, req, yield_requests=True, **kwargs))
            except StopIteration:
                pass

        raise gen.Return(r)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号