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


Python CookieJar.extract_cookies方法代码示例

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


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

示例1: extract_cookiejar

# 需要导入模块: from six.moves.http_cookiejar import CookieJar [as 别名]
# 或者: from six.moves.http_cookiejar.CookieJar import extract_cookies [as 别名]
 def extract_cookiejar(self):
     jar = CookieJar()
     # self._respose could be None
     # if this method is called from custom preapre response
     # function called from spider cache backend
     if self._response and self._request:
         jar.extract_cookies(
             # pylint: disable=protected-access
             MockResponse(self._response._original_response.msg),
             # pylint: enable=protected-access
             MockRequest(self._request),
         )
     return jar
开发者ID:lorien,项目名称:grab,代码行数:15,代码来源:urllib3.py

示例2: extract_cookiejar

# 需要导入模块: from six.moves.http_cookiejar import CookieJar [as 别名]
# 或者: from six.moves.http_cookiejar.CookieJar import extract_cookies [as 别名]
 def extract_cookiejar(self, resp, req):
     jar = CookieJar()
     jar.extract_cookies(MockResponse(resp._original_response.msg),
                         MockRequest(req))
     return jar
开发者ID:ad-m,项目名称:grab,代码行数:7,代码来源:urllib3.py

示例3: HttpTransport

# 需要导入模块: from six.moves.http_cookiejar import CookieJar [as 别名]
# 或者: from six.moves.http_cookiejar.CookieJar import extract_cookies [as 别名]
class HttpTransport(Transport):
    """
    HTTP transport using urllib2.  Provided basic http transport
    that provides for cookies, proxies but no authentication.
    """

    def __init__(self, **kwargs):
        """
        @param kwargs: Keyword arguments.
            - B{proxy} - An http proxy to be specified on requests.
                 The proxy is defined as {protocol:proxy,}
                    - type: I{dict}
                    - default: {}
            - B{timeout} - Set the url open timeout (seconds).
                    - type: I{float}
                    - default: 90
        """
        Transport.__init__(self)
        Unskin(self.options).update(kwargs)
        self.cookiejar = CookieJar()
        self.proxy = {}
        self.urlopener = None

    def open(self, request):
        try:
            url = request.url
            log.debug('opening (%s)', url)
            u2request = urllib.request.Request(url)
            self.proxy = self.options.proxy
            return self.u2open(u2request)
        except urllib.error.HTTPError as e:
            raise TransportError(str(e), e.code, e.fp)

    def send(self, request):
        result = None
        url = request.url
        msg = request.message
        headers = request.headers
        try:
            u2request = urllib.request.Request(url, msg, headers)
            self.addcookies(u2request)
            self.proxy = self.options.proxy
            request.headers.update(u2request.headers)
            log.debug('sending:\n%s', request)
            fp = self.u2open(u2request)
            self.getcookies(fp, u2request)
            result = Reply(200, fp.headers.__dict__, fp.read())
            log.debug('received:\n%s', result)
        except urllib.error.HTTPError as e:
            if e.code in (202, 204):
                result = None
            else:
                raise TransportError(e.msg, e.code, e.fp)
        return result

    def addcookies(self, u2request):
        """
        Add cookies in the cookiejar to the request.
        @param u2request: A urllib2 request.
        @rtype: u2request: urllib2.Requet.
        """
        self.cookiejar.add_cookie_header(u2request)

    def getcookies(self, fp, u2request):
        """
        Add cookies in the request to the cookiejar.
        @param u2request: A urllib2 request.
        @rtype: u2request: urllib2.Requet.
        """
        self.cookiejar.extract_cookies(fp, u2request)

    def u2open(self, u2request):
        """
        Open a connection.
        @param u2request: A urllib2 request.
        @type u2request: urllib2.Requet.
        @return: The opened file-like urllib2 object.
        @rtype: fp
        """
        tm = self.options.timeout
        url = self.u2opener()
        return url.open(u2request, timeout=tm)

    def u2opener(self):
        """
        Create a urllib opener.
        @return: An opener.
        @rtype: I{OpenerDirector}
        """
        if self.urlopener is None:
            return urllib.request.build_opener(*self.u2handlers())
        else:
            return self.urlopener

    def u2handlers(self):
        """
        Get a collection of urllib handlers.
        @return: A list of handlers to be installed in the opener.
        @rtype: [Handler,...]
        """
#.........这里部分代码省略.........
开发者ID:chatoooo,项目名称:suds-ng,代码行数:103,代码来源:http.py


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